Removing glare with opencv and keeping edges removed from the glare - python

I have this image : https://imgur.com/9A7542w
And i am trying to get the contours of the image but as we can see in this : https://imgur.com/VU7KqiS
where there is glare, the contours of some circles are not drawn.
I assume that if i get the glare off on this photo, when i use canny, the edges will be drawn correctly?
I am new to openCV, i've read some post on here and tried out some techniques, but it didn't work out at all.
Note: i am doing this in python
Anyone could help ? Thanks a lot.
What i tried first :
import cv2 as cv
img = cv.imread('Photos/board.jpg')
canny = cv.Canny(img, 125, 175)
contours, hierarchies = cv.findContours(canny, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)
blank = np.zeros(img.shape, dtype='uint8')
cv.drawContours(blank, contours, -1, (0,0,255),1)
cv.imshow('Contours drawn', blank)
results : https://imgur.com/yLVFCh2
Second attempt a bit better but useless things appears in the result
adaptive_thresh = cv.adaptiveThreshold(gray, 255, cv.ADAPTIVE_THRESH_MEAN_C, cv.THRESH_BINARY, 13, 2)
cv.imshow('Adaptive thresholding', adaptive_thresh)
canny = cv.Canny(adaptive_thresh, 125, 175)
cv.imshow('Canny edges', canny)
Results (Theres so much white pixels that appear on the photo : https://imgur.com/mqljl1m

I think the easiest way to do this is to change the 2nd value of your Canny detection like this :
canny = cv.Canny(img, 25, 175)
the lower threshold (second argument) is set lower then you can avoid this glare effect. More info here
From my point of view, you can also work in hsv space which is more confomfortable if you want to extract informations from images with effects like this. More info about hsv. The Fig. 3 a) speaks for itself.
Here is the full code, you had some errors in yours (and maybe you use an old opencv release)
import numpy as np
img = cv.imread(r'C:\Users\MyUser\Desktop\board.jpeg')
canny = cv.Canny(img, 25, 175)
img2, contours, hierarchy = cv.findContours(canny, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)
blank = np.zeros(img.shape, dtype='uint8')
cv.drawContours(blank, contours, -1, (0,0,255),1)
cv.imshow('Contours drawn', blank)
cv.waitKey(0)
EDIT: I also want to tell you that it'll be difficult to use the extracted coordinates here. You'd better use circle detection and line detection to extract and use the coordinates of the board and pucks.

Related

OpenCV - Is there a way to reliably segment images such as this one?

Is there a way to reliably segment product images similar to this one? Even just separating the 3 variations from the border would be great. The problem is that the image touches the border, and I don't know how to deal with that! Not all images are alike, so I need something highly adaptable.
These were the results I achieved using https://docs.opencv.org/master/d3/db4/tutorial_py_watershed.html. My code is identical to the tutorial.
Underwear:
Camera:
What I expected to achieve instead, at least for the image containing the underwear and camera equipment, since the other one is a lot more complex, is for every single object in the image that is not touching another object to be selected separately and outlined in blue. It seems some of the underwear were properly selected as I expected (the first one minus the elastic band) and the first one in the second row (perfectly).
You can use contour as you were going for and take it from the outside. Since the borders are white you invert the threshold so you'll have something like this:
import numpy as np
import cv2 as cv
im = cv.imread('5zdA0.jpg')
imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)
cv.imshow('image', imgray)
cv.waitKey(0)
ret, thresh = cv.threshold(imgray, 160, 255, 1)
contours, hierarchy = cv.findContours(thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(imgray, contours, -1, (0,255,0), 3)
cv.imshow('image', imgray)
cv.waitKey(0)
You'll have to tune these parameters for your images but this should get you going

Rectangle detection inaccuracy using approxPolyDP() in openCV

As part of a program which contains a series of images to be processed, I first need to detect a green-coloured rectangle. I'm trying to write a program that doesn't use colour masking, since the lighting and glare on the images will make it difficult to find the appropriate HSV ranges.
(p.s. I already have two questions based on this program, but this one is unrelated to those. It's not a follow up, I want to address a separate issue.)
I used the standard rectangle detection technique, making use of findContours() and approxPolyDp() methods. I added some constraints that got rid of unnecessary rectangles (like aspectRatio>2.5, since my desired rectangle is clearly the "widest" and area>1500, to discard random small rectangles) .
import numpy as np
import cv2 as cv
img = cv.imread("t19.jpeg")
width=0
height=0
start_x=0
start_y=0
end_x=0
end_y=0
output = img.copy()
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
#threshold
th = cv.adaptiveThreshold(gray,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,9,2)
cv.imshow("th",th)
#rectangle detection
contours, _ = cv.findContours(th, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
for contour in contours:
approx = cv.approxPolyDP(contour, 0.01* cv.arcLength(contour, True), True)
cv.drawContours(img, [approx], 0, (0, 0, 0), 5)
x = approx.ravel()[0]
y = approx.ravel()[1]
x1 ,y1, w, h = cv.boundingRect(approx)
a=w*h
if len(approx) == 4 and x>15 :
aspectRatio = float(w)/h
if aspectRatio >= 2.5 and a>1500:
print(x1,y1,w,h)
width=w
height=h
start_x=x1
start_y=y1
end_x=start_x+width
end_y=start_y+height
cv.rectangle(output, (start_x,start_y), (end_x,end_y), (0,0,255),3)
cv.putText(output, "rectangle "+str(x1)+" , " +str(y1-5), (x1, y1-5), cv.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0))
cv.imshow("op",output)
print("start",start_x,start_y)
print("end", end_x,end_y)
print("width",width)
print("height",height)
It is working flawlessly for all the images, except one:
I used adaptive thresholding to create the threshold, which was used by the findContours() method.
I tried displaying the threshold and the output , and it looks like this:
The thresholds for the other images also looked similar...so I can't pinpoint what exactly has gone wrong in the rectangle detection procedure.
Some tweaks I have tried:
Changing the last two parameters in the adaptive parameters method.
I tried 11,1 , 9,1, and for both of them, the rectangle in the
threshold looked more prominent : but in this case the output
detected no rectangles at all.
I have already disregarded otsu thresholding, as it is not working
for about 4 of my test images.
What exactly can I tweak in the rectangle detection procedure for it to detect this rectangle?
I also request , if possible, only slight modifications to this method and not some entirely new method. As I have mentioned, this method is working perfectly for all of my other test images, and if the new suggested method works for this image but fails for the others, then I'll find myself back here asking why it failed.
Edit: The method that abss suggested worked for this image, however failed for:
image 4
image 1, far off
Other test images:
image 1, normal
image 2
image 3
image 9, part 1
image 9, part 2
You can easily do it by adding this line of code after your threshold
kernel = cv.getStructuringElement(cv.MORPH_RECT,(3,3))
th = cv.morphologyEx(th,cv.MORPH_OPEN,kernel)
This will remove noise within the image. you can see this link for more understanding about morphologyEx https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html
The results I got is shown below
I have made a few modifications to your code so that it works with all of your test images. There are a few false positives that you may have to filter based on HSV color range for green (since your target is always a shade of green). Alternately you can take into account the fact that the one of the child hierarchy of your ROI contour is going to be > 0.4 or so times than the outer contour. Here are the modifications:
Used DoG for thresholding useful contours
Changed arcLength multiplier to 0.5 instead of 0.1 as square corners are not smooth
cv2.RETR_CCOMP to get 2 level hierarchy
Moved ApproxPolyDP inside to make it more efficient
Contour filter area changed to 600 to filter ROI for all test images
Removed a little bit of unnecessary code
Check with all the other test images that you may have and modify the parameters accordingly.
img = cv2.imread("/path/to/your_image")
width=0
height=0
start_x=0
start_y=0
end_x=0
end_y=0
output = img.copy()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gw, gs, gw1, gs1, gw2, gs2 = (3,1.0,7,3.0, 3, 2.0)
img_blur = cv2.GaussianBlur(gray, (gw, gw), gs)
g1 = cv2.GaussianBlur(img_blur, (gw1, gw1), gs1)
g2 = cv2.GaussianBlur(img_blur, (gw2, gw2), gs2)
ret, thg = cv2.threshold(g2-g1, 127, 255, cv2.THRESH_BINARY)
contours, hier = cv2.findContours(thg, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
img_cpy = img.copy()
width=0
height=0
start_x=0
start_y=0
end_x=0
end_y=0
for i in range(len(contours)):
if hier[0][i][2] == -1:
continue
x ,y, w, h = cv2.boundingRect(contours[i])
a=w*h
aspectRatio = float(w)/h
if aspectRatio >= 2.5 and a>600:
approx = cv2.approxPolyDP(contours[i], 0.05* cv2.arcLength(contours[i], True), True)
if len(approx) == 4 and x>15 :
width=w
height=h
start_x=x
start_y=y
end_x=start_x+width
end_y=start_y+height
cv2.rectangle(img_cpy, (start_x,start_y), (end_x,end_y), (0,0,255),3)
cv2.putText(img_cpy, "rectangle "+str(x)+" , " +str(y-5), (x, y-5), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 0))
plt.imshow(img_cpy)
print("start",start_x,start_y)
print("end", end_x,end_y)

how to draw outlines of objects on an image to a separate image

i am working on a puzzle, my final task here is to identify edge type of the puzzle piece.
as shown in the above image i have mange to rotate and crop out every edge of the piece in same angle. my next step is to separate the edge line into a separate image like as shown in the image bellow
then to fill up one side of the line with with a color and try to process it to decide what type of edge it is.
i dont see a proper way to separate the edge line from the image for now.
my approach::
one way to do is scan pixel by pixel and find the black pixels where there is a nun black pixel next to it. this is a code that i can implement. but it feels like a primitive and a time consuming approach.
so if there you can offer any help or ideas, or any completely different way to detect the hollows and humps.
thanks in advance..
First convert your color image to grayscale. Then apply a threshold, say zero to obtain a binary image. You may have to use morphological operations to further process the binary image if there are holes. Then find the contours of this image and draw them to a new image.
A simple code is given below, using opencv 4.0.1 in python 2.7.
bgr = cv2.imread('puzzle.png')
gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)
_, roi = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)
cv2.imwrite('/home/dhanushka/stack/roi.png', roi)
cont = cv2.findContours(roi, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
output = np.zeros(gray.shape, dtype=np.uint8)
cv2.drawContours(output, cont[0], -1, (255, 255, 255))
# removing boundary
boundary = 255*np.ones(gray.shape, dtype=np.uint8)
boundary[1:boundary.shape[0]-1, 1:boundary.shape[1]-1] = 0
toremove = output & boundary
output = output ^ toremove

Identify a line on an image using OpenCV

This is the frame I want to process. I wanted to identify a line in an image. Although, I did that via Canny Edge detection, I tried using the corners too (expecting the dots to cover most of the lines). Counter-intuitively, more dots appeared on the noise in the image than the actual line. I was wondering if anyone knows of a function in OpenCV (Python) to connect these dots intelligently, only to connect over the line, and not the noise.
Help will be much appreciated.
I wanted to identify this black line
import cv2
import numpy as np
img = cv2.imread('fw1.jpeg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
minLineLength = 100
maxLineGap = 10
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap)
for x1,y1,x2,y2 in lines[0]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
cv2.imwrite('houghlines5.jpg',img)
I think you should increase contrast and get rid of noise in the first place. Contrast increase is described here: https://stackoverflow.com/a/46522515/8682088
Code from source above:
cv::Mat img = cv::imread("E:\\Workspace\\KS\\excercise\\oBwBH.jpg", 0);
cv::Mat workingMat;
cv::GaussianBlur(img, workingMat, cv::Size(101, 101), 31, 31); //high blur to extract background light
img = img - 0.7*work; //adjust light level
cv::normalize(img, img, 0, 255, cv::NORM_MINMAX); \\use whole range
cv::medianBlur(img, img, 5); \\remove noise
cv::Canny(img, work, 100, 200); \\extract lines; you could do hough lines instead since it has canny inside.
After that I suggest using HoughLines or HoughLinesP in opencv library.

Opencv : TypeError: contour is not a numpy array, neither a scalar

I'm trying to figure out the area of greenery in my area. I have taken image at 50 metre zoom level from Google Maps and tried separating green color from the image.
These have been taken only for testing purpose. Will perform on high resolution images, if things show good results.
Original Image :
Note : I need to reduce it's dimension for uploading because of 2 MB max size restriction.
Image after drawing contours on it :
Following is my code :
import numpy as np
import cv2
image = cv2.imread('map.png')
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# lower and upper range of green color in HSV color format
mask = cv2.inRange(hsv, np.array([75, 50, 0]), np.array([120, 100, 100]))
mask, contours, hierarchy = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
# print cv2.contourArea(contours)
image_with_contours = cv2.drawContours(image, contours, -1, (0,255,0), 3)
cv2.imshow('CHAIN_APPROX_NONE', image_with_contours)
cv2.waitKey(0)
cv2.destroyAllWindows()
My Problems :
What is an effective way for finding the lower and upper HSV range of a color. I found some HSV color picker but they were not of much help. Even after taking [75, 50, 0] as lower bound and [120, 100, 100] as upper bound i can see some parts of green color missed in the second image with contour.
Area calculation using print cv2.contourArea(contours) shows the following error TypeError: contour is not a numpy array, neither a scalar
Following didn't help me :
OpenCV TypeError: contour is not a numpy array, neither a scalar
Thanks
In general for remote sensing, they use multi spectral, hyper spectral images to calculate the vegetation. Operating in rgb/hsv will not be so accurate. Try if you can find one.
Anyways, for the first part, its better you make an opencv track bar to find the proper hsv values. An example can be found here for rgb . As for the second part, contours is a list of all the contours found. So use-
for i in range len(contours):
print cv2.contourArea(contours[i])

Categories