Hello, I am new to the opencv world. I would like to detect a bending edge by line laser triangulation. I have a picture with the line of the laser and would now like to edit the image so I only see an averaged lines (drawContour). The image was recorded only with the laser. The line is a little intertwined.
Now my questions / thought process: How can I convert the binarized (threshold) picture so that the line of a color value (everything white) and the rest is black?
Then I need a mean value (min-max, x, y) of all values to characterize a line with the draw function. Is there a function for this?
This new mean value line then has to be in an array that I can do the triangulation. np.array []?
In my previous code I have tried some filters. Unfortunately, I do not come to the result which I imagine. Which filter would be the best? And how do I get the rest over a certain threshold on white? How can I make pixels black which should not be there? What kind of problems do I have? Do I go wrong? Please help me, over every contribution I would be very happy.
Thanks Daniel
My Code:
import cv2
import numpy as np
import matplotlib.pyplot as plt
Img1=cv2.imread('/home/pi/Desktop/6.jpg',0)
Img1=cv2.resize(Img1,(0,0),fx=0.5,fy=0.5)
#Thresh-Binär
retval,th1=cv2.threshold(Img1,30,255,cv2.THRESH_BINARY)
#Thres- Otsu
retval2,th2=cv2.threshold(Img1,30,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#Thresh-Adaptive+Gauss
th3=cv2.adaptiveThreshold(Img1,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, \
cv2.THRESH_BINARY,3,2)
#Gauss
blur=cv2.GaussianBlur(Img1,(5,0),4)
blur2=cv2.GaussianBlur(th1,(5,5),0)
kernel=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(10,10))
skinmask=cv2.erode(Img1,kernel,iterations=3)
skinmask=cv2.dilate(Img1,kernel,iterations=3)
skinmask=cv2.GaussianBlur(skinmask,(3,3),1)
#Morpholo Filter/noise removal
kernel1 = np.ones((3,2),np.uint8) #np.unit=unsigned interger(0-255)
opening = cv2.morphologyEx(blur,cv2.MORPH_OPEN,kernel, iterations = 2)
#Contour finder
im2,contours,hierarchy=cv2.findContours(skinmask,cv2.RETR_TREE,cv2.CHAIN_APPRO X_SIMPLE)
#kanten = cv2.Canny(opening,100,50)
#draw Contours
cnt=contours[0]
M=cv2.moments(cnt) #??
print(M)
Kontur=cv2.drawContours(Img1,contours,0,(255,255,255),5)
perimeter=cv2.arcLength(cnt,True)
cv2.imshow('Original',Img1)
cv2.imshow('Tresh',th1)
cv2.imshow('Tresh+Otsu',th2)
#cv2.imshow('TreshAdaptiv+Gauss',th3)
cv2.imshow('Kontur',Kontur)
#cv2.imshow('Tresh+Gaussian',blur2)
cv2.imshow('arc',perimeter)
cv2.imshow('Morph',opening)
cv2.imshow('skin',skinmask)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(len(contours))
Edit 28.05.2017
Example
Related
I'm very new to the image processing and object detection. I'd like to extract/identify the position and dimensions of teeth in the following image:
Here's what I've tried so far using OpenCV:
import cv2
import numpy as np
planets = cv2.imread('model.png', 0)
canny = cv2.Canny(planets, 70, 150)
circles = cv2.HoughCircles(canny,cv2.HOUGH_GRADIENT,1,40, param1=10,param2=16,minRadius=10,maxRadius=80)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
# draw the outer circle
cv2.circle(planets,(i[0],i[1]),i[2],(255,0,0),2)
# draw the center of the circle
cv2.circle(planets,(i[0],i[1]),2,(255,0,0),3)
cv2.imshow("HoughCirlces", planets)
cv2.waitKey()
cv2.destroyAllWindows()
This is what I get after applying canny filter:
This is the final result:
I don't know where to go from here. I'd like to get all of the teeth identified. How can I do that?
I'd really appreciate any help..
Note that the teeth-structure is more-or-less a parabola (upside-down). If you could somehow guess the parabolic shape that defines the centroids of those blobs (teeth), then your problem could be simplified to a reasonable extent. I have shown a red line that passes through the centers of the teeth.
I would suggest you to approach it as follows:
Binarize your image (background=0, else 1). You could use sklearn.preprocessing.binarize.
Calculate the centroid of all the non-zero pixels. This is the central blue circle in the image. Call this structure_centroid. See this: How to center the nonzero values within 2D numpy array?.
Make polar slices of the entire image, centered at the location of the structure_centroid. I have shown a cartoon image of such polar slices (triangular semi-transparent). Cover complete 360 degrees. See this: polarTransform library.
Determine the position of the centroid of the non-zero pixels for each of these polar slices. See these:
find the distance between a point and a curve python.
Find the minimum distance from a point to a curve.
The array containing these centroids gives you the locus (path) of the average location of the teeth. Call this centroid_path.
Run an elimination/selection algorithm on the circles you were able to detect, that are closest to the centroid_path. Use a threshold distance to drop the outliers.
This should give you a good approximation of the teeth with the circles.
I hope this helps.
Firstly ı want to find the change of water dripping time into the fabric with camera.User will drip water to the fabric after that algorithm detect the movement until water absorb completely and plot the graph change-time with showing the absorbation time,area etc..
In order to do detect movement I have used absdif function with constant change rate.And I take frames start time of detection to the end like this image.There is no problem in here.But in order to calculate the absorbation of water I thresholded frame and use countNonZero function to calculate the number of black pixel . But there is one problem here,the black pixels that shown red lines of thresholded images are continuosly changing(like shaking,vibration etc).So plotting process is fail.
Try
I tried to change webcam device(using phone camera by İpcam)
I tried to adaptive threshold methods(otsu etc) to find optimum threshold
Smoothing lightning conditions and capturing without background
Success
When I use the video that was taken by phone camera as input, shaking and vibration effects decreases and I can reach the success this graph as expected
QUESTION
How can I smooth the thresholded image in real time
Another approach
Code
import cv2
from datetime import datetime
import numpy as np
import matplotlib.pyplot as plt
import operator
def pixelHesaplayici(x):
siyaholmayanpixel=cv2.countNonZero(x)
height,width=x.shape
toplampixel=height*width
siyahpixelsayisi=toplampixel-siyaholmayanpixel
return siyahpixelsayisi
def grafikciz(sure,newblackpixlist,maxValue,index,totaltime,cm):
plt.figure(figsize=(15,15))
plt.plot(sure,newblackpixlist)
line,=plt.plot(sure,newblackpixlist)
plt.setp(line,color='r')
plt.text(totaltime/2,maxValue/2, r'$Max-
Pixel=%d$'%maxValue,fontsize=14,color='r')
plt.text(totaltime/2,maxValue/2.5, r'$Max-emilim-
zamanı=%f$'%sure[index],fontsize=14,color='b')
plt.text(totaltime/2,maxValue/3, r'$Max-
Alan=%fcm^2$'%cm,fontsize=14,color='g')
plt.ylabel('Black Pixels')
plt.xlabel('Time(s)')
plt.grid(True)
plt.show()
static_back=None
i=0
blackpixlist=[]
newblackpixlist=[]
t=[]
video=cv2.VideoCapture("kumas1.mp4")
while(True):
ret,frame=video.read()
if ret==True:
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
gray=cv2.GaussianBlur(gray,(5,5),0)
_,threshforgraph=cv2.threshold(gray,0,255,
cv2.THRESH_BINARY+cv2.THRESH_OTSU)
if static_back is None:
static_back=gray
continue
diff_frame=cv2.absdiff(static_back,gray)
threshfortime=cv2.threshold(diff_frame,127,255,cv2.THRESH_BINARY)[1]
#threshfortime=cv2.dilate(threshfortime,None,iterations=2)
(_,cnts,_)=cv2.findContours(threshfortime.copy(),
cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for contour in cnts:
if cv2.contourArea(contour)<450:
continue
an=datetime.now()
t.append(an.minute*60+an.second+(an.microsecond/1000000))
cv2.fillPoly(frame,contour, (255,255,255), 8,0)
cv2.imwrite("samples/frame%d.jpg"%i,threshforgraph)
i+=1
cv2.imshow("org2",frame)
#cv2.imshow("Difference Frame",diff_frame)
#cv2.imshow("Threshold Frame",threshfortime)
#cv2.imshow("Threshforgraph",threshforgraph)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
ti=t[1::3]
lasttime=ti[-1]
firsttime=ti[-len(ti)]
totaltime=lasttime-firsttime
for i in range(0,i):
img=cv2.imread('samples/frame%d.jpg'%i,0)
blackpixlist.append(pixelHesaplayici(img))
ilkpix=blackpixlist[0]
for a in blackpixlist:
newblackpixlist.append(a-ilkpix)
newblackpixlisti=newblackpixlist[1::3]
index , maxValue=max(enumerate(newblackpixlisti),
key=operator.itemgetter(1))
sure=np.linspace(0,totaltime,len(newblackpixlisti))
cm=0.0007*maxValue # For 96 dpi
grafikciz(sure,newblackpixlisti,maxValue,index,totaltime,cm)
What about subtracting the first frame from the next frames? If you can know or can detect when there is no drop and subtract it, the difference will only give you the result of the drop.
This approach might be interesting also if you have several drops in different places and want discard the previous drop.
Note that you can do the subtraction before and after thresholding. I would recommend before thresholding.
If you know that you have a lot of shaking in your process, you probably need to apply a digital stabilization, in which case I would advise to look at this tutorial:
https://www.learnopencv.com/video-stabilization-using-point-feature-matching-in-opencv/
Of course, the stabilization should be done before the subtraction.
In general for your problem, I wouldn't use an adaptive method. The threshold should be the same for all frames, if it adapts depending on the image, you could have invalid results.
I hope I properly understood your problem!
I am trying to detect ellipses in some images.
After some functions I got this edges map:
I tried using Hough transform to detect ellipses, but this transform has very high complexity, so my computer didn't finish running the transform command even after 5 hours(!).
I also tried doing connected components and got this:
In last case I also tried continue and binarized the image.
In all cases I am stuck in these steps, and have no idea how continue from here.
My mission is detect tomatoes in the image. I am approaching this by trying to detect circles and ellipses and find the radius (or average radius in ellipses case) for each one.
edited:
I add my code for the first method (the result is edge map from above):
img = cv2.imread(r'../images/assorted_tomatoes.jpg')
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgAfterLight=lightreduce(img)
imgAfterGamma=gamma_correctiom(imgAfterLight,0.8)
th2 = 255 - cv2.adaptiveThreshold(imgAfterGamma,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,5,3)
median2 = cv2.medianBlur(th2,3)
where median2 is the result of shown above in edge map
and the code for connected components:
import scipy
from scipy import ndimage
import matplotlib.pyplot as plt
import cv2
import numpy as np
fname=r'../images/assorted_tomatoes.jpg'
blur_radius = 1.0
threshold = 50
img = scipy.misc.imread(fname) # gray-scale image
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print(img.shape)
# smooth the image (to remove small objects)
imgf = ndimage.gaussian_filter(gray_img, blur_radius)
threshold = 80
# find connected components
labeled, nr_objects = ndimage.label(imgf > threshold)
where labeled is the result above
another edit:
this is the input image:
Input
The problem is that after edge detection, there are a lot of unnecessary edges in sub regions that disturbing for make smooth edge map
To me this looks like a classic problem for the watershed algorithm. It is designed for segmenting out touching objects like the tomatoes. My example is in Matlab (I'm on the wrong computer today) but it should translate to python easily. First convert to greyscale as you do and then invert the images
I=rgb2gray(img)
I2=imcomplement(I)
The image as is will over segment, so we remove minima that are too shallow. This can be done with the h-minima transform
I3=imhmin(I2,50);
You might need to play with the 50 value which is the height threshold for suppressing shallow minima. Now run the watershed algorithm and we get the following result.
L=watershed(I3);
The results are not perfect. It needs additional logic to remove some of the small regions, but it will give a reasonable estimate. The watershed and h-minima are contained in the skimage.morphology package in python.
I'm using canny algorithm to find the edges.
Next, I want to keep the region inside the closed curves.
My code sample is:
import cv2
import numpy as np
from matplotlib import pyplot as plt
import scipy.ndimage as nd
from skimage.morphology import watershed
from skimage.filters import sobel
img1 = cv2.imread('coins.jpg')
img = cv2.imread('coins.jpg',0)
edges= cv2.Canny(img,120,200)
markers = np.zeros_like(img)
markers[edges<50] = 0
markers[edges==255] = 1
img1[markers == 1] = [0,0,255]
img1[markers == 0] = [255,255,255]
cv2.imshow('Original', img)
cv2.imshow('Canny', img1)
#Wait for user to press a key
cv2.waitKey(0)
My output image is
I want to show the original pixels values inside the coins. Is that possible?
I suggest you use an union-find structure to get the connected components of white pixels of your img1. (You might want to find the details of this algorithm on Wikipedia : https://en.wikipedia.org/wiki/Disjoint-set_data_structure).
Once you have the connected components, my best idea is to consider the conected components that do not contain any point on the border of your picture (they should correspond to the interior of your coins) and color them in the color of img.
Sure, you may have some kind of triangles between your coins that will still be colored, but you could remove the corresponding connected components by hand.
Not really. The coin outlines are not continuous so that any kind of filling will leak.
You can repair the edges by some form of morphological processing (erosion), but this will bring the coins in contact and create unreachable regions between them.
As a fallback solution, you can try a Hough circle detector and mask inside the disks.
I have a problem using the Hu moments for shape recognition. The goal is to be able to recognize the two white circles and the two white squares on the left in the picture.
http://i.stack.imgur.com/wVzYa.jpg
I tried using the cv2.approxPolyDP method but it doesn't quite work when there is a rotation. For the white circles I used the cv2.HoughCircles method and it works pretty well. However, I really need to use the Hu moments, because it seems it is a better method.
I have this code below:
import cv2
import numpy as np
nomeimg = "coded_target.jpg"
img = cv2.imread(nomeimg)
gray = cv2.imread(nomeimg,0)
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
element = cv2.getStructuringElement(cv2.MORPH_CROSS,(4,4))
imgbnbin = thresh
imgbnbin = cv2.dilate(imgbnbin, element)
#find contour
contours,hierarchy=cv2.findContours(imgbnbin,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
#Elimination small contours
Areacontours = list()
for i in Areacontours:
area = cv2.contourArea(contours[i])
if (area > 90 ):
Areacontours.append(contours[i])
contours = Areacontours
print('found objects')
print(len(contours))
print("humoments")
mom = cv2.moments(contours[0])
Humoments = cv2.HuMoments(mom)
Humoments2 = -np.sign(Humoments)*np.log10(np.abs(Humoments))
print(Humoments2)
It returns 7 numbers which are the Hu invariants. I tried rotating the picture and I see that only the last two are changing. It also says that it only found 1 object found when there are obviously more than that. Is it normal?
I thought of using templates for shape identification purposes but I don't know how to do it: I believe I should exploit the Hu moments of the templates and see where it fits but I'm not sure on how to achieve it.
I appreciate the help.
You can create a template image of the squares and implement a template matching technique in order to detect it on the image.
You can also detect the contour of the template image and use the function cv2.matchshapes . However this function is used in order to compare two images. So, I guess you will have to make a window with the same size with you template and run it through you original image in order to detect which part is the best match (minimum value for the function matchshape).