0

I used houghLines transformation to detect lines in an image and I draw the lines detected by that on a separate Mat image using cv::line() function. Note that the lines detected by houghLinesP are usually overlapping with each other and high in number. (even if there is 1 line in the image it will detect number of adjacent lines for it)

for (size_t i = 0; i < lines.size(); i++)
{
    Vec4i l = lines[i];
    line(src, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0, 255, 0), 10, CV_AA);
}

I wanted to draw the lines in solid green color without any transparency. But in the resulting image I noticed that some lines at the edges are transparent. (or the borders of lines are blurred) Following is a part of my resulting image.

enter image description here

I want to avoid this. I want to have sharp edges for the lines and should not have any transparency. How to do it?

Samitha Chathuranga
  • 1,689
  • 5
  • 30
  • 57

1 Answers1

4

The CV_AA flag tell cv::line() to draw an Anti-Aliased line. This is the source of your transparent line border pixels.
If you remove the CV_AA flag, the line will be drawn jagged, but without transparencies (Bresenham).

Looking more closely at the image there are visible JPG artifacts inside the colored area (sporadic darker spots). These artifacts to not exist in the image in memory but will appear in a JPG saved file. Try saving your image as PNG.

Adi Shavit
  • 16,743
  • 5
  • 67
  • 137
  • I removed that flag. But same result as before. – Samitha Chathuranga Jun 28 '15 at 07:36
  • @SamithaChathuranga: In that case it is a bug. Try rebuilding your code again. If you still see this, try writing a small program that just draws a line with the parameters you use and see if it happens. If you can reproduce this bug, then you should submit this as a bug to the OpenCV project. If not, comment back here what the problem was. – Adi Shavit Jun 28 '15 at 07:38
  • I did so but the problem remains. – Samitha Chathuranga Jun 28 '15 at 08:09
  • @SamithaChathuranga: As I said, try writing a small program that reproduces only this problem. Just open a blank image and draw a line on it. – Adi Shavit Jun 28 '15 at 08:18
  • Yes that is what I did. :D I wrote a small program and draw a line in a blank image. The results are same. – Samitha Chathuranga Jun 28 '15 at 08:24
  • Please note that I write the resulting image into a jpeg file and such above blurring is visible only when I open that image file in a photo viewer (eg. Windows photo viewer) and zoom it. May be when displaying the image directly via the program by imshow() this effect is not visible as it should be much zoomed in to see that blurring effect. Anyway can this be an issue with writing Mat image into a file? – Samitha Chathuranga Jun 28 '15 at 08:34
  • 2
    Aha! Indeed, save your images as PNG not JPG. JPG is a lossy compression format which will indeed cause this type of artifact. PNG is lossless and does not add artifacts. Also, Windows Photo Viewer does bilinear interpolation to "improve" the display quality of zoomed images. – Adi Shavit Jun 28 '15 at 08:41
  • 1
    That was the issue. Problem solved when I saved the image in .png format and removing CV_AA flag. Thanks – Samitha Chathuranga Jun 28 '15 at 09:18