How to detect smooth curves in matlab

I am trying to detect a curved conveyor in an image. I used the following code using the Hough transform to detect its edges

%# load image, and process it I = imread('ggp\2.jpg'); g = rgb2gray(I); bw = edge(g,'Canny'); [H,T,R] = hough(bw); P = houghpeaks(H,500,'threshold',ceil(0.4*max(H(:)))); % I apply houghlines on the grayscale picture, otherwise it doesn't detect % the straight lines shown in the picture lines = houghlines(g,T,R,P,'FillGap',5,'MinLength',50); figure, imshow(g), hold on for k = 1:length(lines) xy = [lines(k).point1; lines(k).point2]; deltaY = xy(2,2) - xy(1,2); deltaX = xy(2,1) - xy(1,1); angle = atan2(deltaY, deltaX) * 180 / pi; if (angle == 0) plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green'); % Plot beginnings and ends of lines plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow'); plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red'); end end 

enter image description here

As shown, two straight lines successfully detect the upper and lower edges of the conveyor, but I don’t know how to determine if it is bent or not (in the picture it is bent) and how to calculate the degree of what.

The curve is approximately drawn manually in the figure below (red):

enter image description here

I did not find a code or function for the Hough transform in matlab to detect such smooth curves (for example, polynomials of the 2nd degree: y= a*x^2 ). Any other solution is also welcome.

This is the original image: enter image description here

+5
source share
1 answer

Looking at your straight lines that reveal the conveyor belt, I assume that you can focus your processing around the area of ​​interest (lines 750 to 950 on the image).
Based on this:

 oimg = imread('http://i.stack.imgur.com/xfXUS.jpg'); %// read the image gimg = im2double( rgb2gray( oimg( 751:950, :, : ) ) ); %// convert to gray, only the relevant part fimg = imfilter(gimg, [ones(7,50);zeros(1,50);-ones(7,50)] ); %// find horizontal edge 

Select only strong horizontal extreme pixels around the center of the area.

 [row, col] = find(abs(fimg)>50); sel = row>50 & row < 150 & col > 750 & col < 3250; row=row(sel); col=col(sel); 

Set a polynomial of the second degree and a line to these points of edges

 [P, S, mu] = polyfit(col,row,2); [L, lS, lmu] = polyfit(col, row, 1); 

Calculate calculated curves

 xx=1:4000; figure;imshow(oimg,'border','tight'); hold on; plot(xx,polyval(P,xx,[],mu)+750,'LineWidth',1.5,'Color','r'); plot(xx,polyval(L,xx,[],lmu)+750,':g', 'LineWidth', 1.5); 

Result

enter image description here

You can visually evaluate how the 2nd degree P better for the conveyor belt border. Looking at the first ratio

 >> P(1) ans = 1.4574 

You see that the coefficient of the x^2 curve is not negligible, making the curve distinctly not straight.

+3
source

All Articles