Recent

Author Topic: Bug When drawing a line in OpenCV  (Read 436 times)

PeterBalch

  • New Member
  • *
  • Posts: 14
Bug When drawing a line in OpenCV
« on: June 22, 2019, 07:05:14 pm »
Whenever I try to draw a line in OpenCV, it is drawn very thick. I ask for 1 pixel wide and get 40 pixels wide. If I draw CV_RGB(255,0,0) I get a fat red line. If I draw CV_RGB(0,255,0) I get a fat green line. If I draw CV_RGB(0,0,255) the program crashes. It doesn't matter whether I'm drawing on an image created with cvCaptureFromCam or by cvLoadImage. It doesn't matter whether I'm using cvLine or cvRectangle.

I suspect it's the usual clash-of-versions problem that I always run up against when I try to use Linux. It looks to me like the Pascal wrapper doesn't match the code in the the OpenCV library.

I'm using Raspberry Pi 3B+, Raspbian 9.9, Lazarus 2.0.0, Pi Camera.

I got a pre-compiled version of OpenCV from "https://github.com/jabelone/OpenCV-for-Pi/raw/master/latest-OpenCV.deb". Python assures me I'm using version 3.4.4 of OpenCV. I've no idea how to find what Lazarus thinks it's connected to. I presume it's the same version.

OpenCV works fine under Python. I was doing Haar face detection which works well. It draws lines just like it's been told to.

I got a Pascal wrapper for OpenCV from "https://github.com/t-edson/MiOpenCV". It was the only wrapper I could find that compiled, linked and ran for Raspberry Pi.

I did the same sort of face detection in Lazarus. Once again, the face detection works well. It finds faces and draws boxes round them. The miOpenCv library contains a sample program called "Demo4_FaceDetect". The output is shown in the attached image. You can see how wide the lines are.

A typical call is:
Code: Pascal  [Select]
  1. cvRectangle(temp, pt1, pt2, CV_RGB(255, 0, 0), 3, 8, 0);

I've also written a face detection program that works with live images for the camera with the same result: fat lines.

These are exactly the symptoms I've had in the past when writing Delphi wrappers for C libraries and you get the function parameters wrong. Clearly, an awful lot of OpenCV is working but line drawing goes wrong.

Any thoughts as to what might be happening? What wrappers do you use?

Peter
« Last Edit: June 23, 2019, 02:47:06 pm by PeterBalch »

engkin

  • Hero Member
  • *****
  • Posts: 2513
Re: Bug When drawing a line in OpenCV
« Reply #1 on: June 22, 2019, 08:32:06 pm »
I got a Pascal wrapper for OpenCV from "https://github.com/jotasvec/miOpenCv/blob/master/README.md". It was the only wrapper I could find that compiled, linked and ran for Raspberry Pi.
I do not see Pascal.

PeterBalch

  • New Member
  • *
  • Posts: 14
Re: Bug When drawing a line in OpenCV
« Reply #2 on: June 23, 2019, 02:43:36 pm »
Oops, sorry. The correct link is:

https://github.com/t-edson/MiOpenCV

(I'll also edit the OP so I don't use up anyone else's time.)

The declaration for cvLine in that version of OpenCV.pas is:

Code: Pascal  [Select]
  1.  procedure  cvLine( img: PCvArr; pt1: CvPoint; pt2: CvPoint;
  2.    color: CvScalar; thickness: longint = 1;
  3.    line_type: longint = 8; shift: longint = 0); cdecl;

And the types are:

Code: Pascal  [Select]
  1.  CvPoint = record
  2.    x : longint;
  3.    y : longint;
  4.  end;
  5.  
  6.  CvScalar = record
  7.    val : array[0..3] of double;
  8.  end;

The equivalent declarations in OpenCV (imgproc_c.h, types_c.h) are:

Code: C  [Select]
  1. CVAPI(void)  cvLine( CvArr* img, CvPoint pt1, CvPoint pt2,
  2.   CvScalar color, int thickness CV_DEFAULT(1),
  3.   int line_type CV_DEFAULT(8), int shift CV_DEFAULT(0) );
  4.  
  5. typedef struct CvPoint
  6. {
  7.   int x;
  8.   int y;
  9. }
  10.  
  11. typedef struct CvScalar
  12. {
  13.   double val[4];
  14. }

which looks about right to me - but something's going wrong.

Peter


Edson

  • Hero Member
  • *****
  • Posts: 1055
Re: Bug When drawing a line in OpenCV
« Reply #3 on: July 25, 2019, 04:59:03 pm »
I got a Pascal wrapper for OpenCV from "https://github.com/t-edson/MiOpenCV". It was the only wrapper I could find that compiled, linked and ran for Raspberry Pi.

Hi. When I was looking for a cross-platform OpenCV Pascal wrapper, I couldn't find some that run in Windows and Linux properly, that's why I wrote the mine. I haven't tested in in Raspberry, only in Ubuntu and Windows. In all the cases I haven't experimented the error you comment. I suspect it has to be with the OpenCV version you are using or with the way FPC call a *.so in a Raspberry. Maybe if you use a big number in "thickness" or change the bytes positions.

By the way, you can use https://github.com/t-edson/LazarusOpenCV too. It's similar to https://github.com/t-edson/MiOpenCV, but cover more OpenCV functions.
Lazarus 1.6 - FPC 3.0.0 - x86_64-win64 on  Windows 7