Azzera filtri
Azzera filtri

How can I crop between 2 verticle lines in an image

1 visualizzazione (ultimi 30 giorni)
I am able to detect the vertical lines of the image using the example from https://uk.mathworks.com/help/images/hough-transform.html. However I need to select 2 specific vertical lines and then crop to leave the image of only what's between them. This is because I have lots of similar images, about 150, and am currently having to manually crop each picture.
Any help would be greatly appreciated!
  2 Commenti
Image Analyst
Image Analyst il 3 Lug 2018
Usually people attach the image and code when they ask for image processing advice. Without that about all I can say is to extract/crop the image between line1 and line2 do this:
croppedImage = yourImage(line1:line2, :, :);
Mattinator
Mattinator il 4 Lug 2018
Sorry, new to this!
This is the code I currently have.
I need to detect the test tube as it can move slightly from image to image, and then crop to leave only the test tube. Not sure if I have started going about this the correct way but thought that detecting the parallel lines of the tube would be the best start? Also would have to put in a for loop to enable to run on all the images in a file as opposed to a single image at a time.
I = imread('image 7.jpg');
figure
imshow(I)
rotI=rgb2gray(I);
BW = edge(rotI);
figure
imshow(BW)
[H,theta,rho] = hough(BW);
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(BW,theta,rho,P,'FillGap',5,'MinLength',7);
figure, imshow(I), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
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');
% Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
% highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','red');

Accedi per commentare.

Risposte (1)

Constantino Carlos Reyes-Aldasoro
This should be rather simple if you find edges, you could use hough or alternatively canny, so try something like
image7 = imread('/Users/ccr22/Desktop/image 7.jpg'); a=edge(image7(150:300,:,1),'canny',[],3); plot(sum(a))
This will detect edges along your image, then you can add them over the vertical dimension and this will produce a series of peaks where your lines of interest exist (notice that I cropped top and bottom where there are some distracting lines). Use findpeaks to detect the peaks and with find you can determine the position along the axis. Then crop as in the above comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by