How can I crop rectangles from an image?

Hello, I am trying, given an image that contains a grid, to detect it in some way to later be able to crop it and keep it, in my case it is an electrocardiogram, I attach image.
I tried apply regionprops, hough, etc.
Thanks in advance!

1 Comment

What are the actual goals?
Do you just need to process this one image, or do you need a method to automatically process any image printed in the same manner?
What all do you actually need to do with the image?
  • crop the region?
  • correct for perspective/barrel distortion?
  • back-calculate the actual signal vectors?

Sign in to comment.

 Accepted Answer

If I were to answer the question as asked:
inpict = imread('image.jpeg');
% get mask selecting red lines
yccpict = rgb2ycbcr(inpict);
limits = [50 200; 0 255; 135 153];
limits = permute(limits,[2 3 1]);
mask = all(yccpict >= limits(1,:,:) & yccpict <= limits(2,:,:),3);
% get rid of stray blobs
mask = bwareafilt(mask,1);
% get blob location, crop image
S = regionprops(mask,'boundingbox');
outpict = imcrop(inpict,S.BoundingBox);
imshow(outpict)

5 Comments

Sorry for taking so long to reply, I didn't really specify but I would need you to crop it to any similar image, the solution you propose would not work for the image I attached, but I think I can make it work, if I get it I will send the fix.
Thank you very much for your contribution!
What is the use case? Why do you not have control over your image capture situation? For example why did you take a photo where the camera is not directly overhead and your lighting is very bad (shadows, etc.)? Why not just use a flatbed scanner to take images? They'll be so much better.
The thing is, I'm going to create an android app that, when a doctor photographs a electrocardiogram, it shows information about it, but the photo can be taken from distinct points and the thing is that I'm triying to solve the problem so they dont have to use a scanner and (in almost all situations) the image is read correctly when they use it fast.
I can think of lots of ways to approach this but none of them are going to be quick and easy. Nothing in a page full of code. For starters assume that the paper is on a consistent background. Then try to identify the background characteristics (color and texture) and segment that out. Then take the inside (the chart) and find the corners and warp it. Some attached demos and web sites might help.
Oh okay, im gonna try that, thank you very much!

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!