Clear Filters
Clear Filters

How detect, crop and straighten a particular shape

6 views (last 30 days)
Hi all, I have to detect, crop and straighten this particular rectangular shape with rounded corner from the image, in order to classify this "film" used in dosimetry application.
These are different films belonging to different class:
I have already tried with common methods like is shown in these posts without any useful result:
Any suggestion?

Accepted Answer

Image Analyst
Image Analyst on 15 Jul 2016
See attached m-file that produces the figure below:
  1 Comment
riccardo isi
riccardo isi on 15 Jul 2016
Thank you again!! I have just modified two lines: when I have to rotate the image I choose the bilinear method because the image seems to keep a better quality, and I have added an if/else to choose the angle because some of these films sometimes are oriented in the opposite way.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 12 Jul 2016
Look at the outer band of the image, like a rectangular "ring" within 40 pixels (or whatever) of the image. Take the mode of those pixels in each color band to get the most common color. Create masks for each color channel and then combine them to form an overall mask. Then invert that to find blobs that are not the background color. Then fill holes with imfill() and get the convex hull. That will get you the rounded rectangle. Then call bwboundaries to get the coordinates and identify the flat parts to get the angle, then call imrotate() to rotate/fix it.
  1 Comment
riccardo isi
riccardo isi on 14 Jul 2016
Edited: riccardo isi on 14 Jul 2016
Thank you so much for your advices. I tried to follow, but I notice two problems:
1) I do not understand how get the angle for rotate the image.
2) There is some manner to remove the two supports that hold the film? I thought about some morphological operator like the erosion/dilation but it didn't work so good...
below there is the code I used:
img = imread('Test_0577.tif');
red = img(:,:,1); % Red channel
green = img(:,:,2); % Green channel
blue = img(:,:,3); % Blue channel
tollerance = 7;
figure; imshow(img), title('RGB image');
%%Red mask
r_mode = mode(red);
r_max1 = max(r_mode(:,1:40));
r_min1 = min(r_mode(:,1:40));
r_max2 = max(r_mode(:,1565:1605));
r_min2 = min(r_mode(:,1565:1605));
r_max = max(r_max1, r_max2)+tollerance;
r_min = min(r_min1, r_min2)-tollerance;
r_mask = zeros(size(red));
for k = 1 : size(r_mask,1)
for i = 1 : size(r_mask,2)
if (red(k,i)>r_min && red(k,i)<r_max)
r_mask(k,i) = 1;
end
end
end
%%Green mask
g_mode = mode(green);
g_max1 = max(g_mode(:,1:40));
g_min1 = min(g_mode(:,1:40));
g_max2 = max(g_mode(:,1565:1605));
g_min2 = min(g_mode(:,1565:1605));
g_max = max(g_max1, g_max2)+tollerance;
g_min = min(g_min1, g_min2)-tollerance;
g_mask = zeros(size(green));
for k = 1 : size(g_mask,1)
for i = 1 : size(g_mask,2)
if (green(k,i)>g_min && green(k,i)<g_max)
g_mask(k,i) = 1;
end
end
end
%%Blue mask
b_mode = mode(blue);
b_max1 = max(g_mode(:,1:40));
b_min1 = min(g_mode(:,1:40));
b_max2 = max(g_mode(:,1565:1605));
b_min2 = min(g_mode(:,1565:1605));
b_max = max(b_max1, b_max2)+tollerance;
b_min = min(b_min1, b_min2)-tollerance;
b_mask = zeros(size(blue));
for k = 1 : size(b_mask,1)
for i = 1 : size(b_mask,2)
if (blue(k,i)>b_min && blue(k,i)<b_max)
b_mask(k,i) = 1;
end
end
end
figure; imshow(r_mask), title('red mask');
figure; imshow(g_mask), title('green mask');
figure; imshow(b_mask), title('blue mask');
%%Overall mask
mask = zeros(size(red));
for k = 1 : size(mask,1)
for i = 1 : size(mask,2)
if (r_mask(k,i) == 1 || b_mask(k,i) == 1 || g_mask(k,i) == 1)
mask(k,i) = 1;
end
end
end
mask = imfill(mask, 'holes');
mask = ~mask;
figure; imshow(mask), title('overall mask');
%%Find boundaries
[B,L] = bwboundaries(mask,'noholes');
figure; imshow(img), title('image with boundaries');
hold on;
for i = 1:length(B)
boundary = B{i};
plot(boundary(:,2), boundary(:,1), 'r', 'LineWidth', 3)
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!