I'm new to matlab, I have a image and i want to crop all the three circles and store them. My code works for single circle in an image. But fails to work when i have more circles in image.
My code:
im=imread('D:\capture.png');
im_gray = rgb2gray(im);
BW = im2bw(im_gray, graythresh(im));
se = strel('disk',3);
bw2=imopen(BW,se);
bw2=~bw2;
s = regionprops(bw2, 'BoundingBox');
rectangle('Position', s.BoundingBox);
imCrop = imcrop(bw2, s.BoundingBox);
figure, imshow(imCrop);
Any ideas about it?
You almost have it working. Bear in mind that when you do s.BoundingBox by itself, you are only extracting the first circle. As such, I would recommend you make a cell array that stores the individual circles for each bounding box, then run a for loop through all of your bounding boxes. As such, each element in your cell array would be a cropped circle. As such, try doing this:
%// Your code
im=imread('D:\capture.png');
im_gray = rgb2gray(im);
BW = im2bw(im_gray, graythresh(im));
se = strel('disk',3);
bw2=imopen(BW,se);
bw2=~bw2;
s = regionprops(bw2, 'BoundingBox');
%// New code here
circles = cell(1,numel(s));
for idx = 1 : numel(s)
rect = s(idx).BoundingBox;
circles{idx} = imcrop(bw2, rect);
end
circles will now be a cell array of cropped circles. To access the ith circle, simply do:
imCrop = circles{i};
Edit
From your comments, you want to detect the largest and smallest circles. This can easily be done by checking the Area attribute from regionprops. You would find the bounding box that generates the minimum and maximum areas. You would need to modify your regionprops call to include the Area flag. As such:
s = regionprops(bw2, 'BoundingBox', 'Area');
[~,indMin] = min([s.Area]);
[~,indMax] = max([s.Area]);
circleSmall = circles{indMin};
circleLarge = circles{indMax};
The above code will find the circles with the minimum and maximum area, then extract those corresponding circles, assuming you've run the code to extract all of those circles in that for loop I wrote earlier. Bear in mind that I had to enclose s.Area in square braces. The reason why is because when you do this, you'll be able to extract all of the areas as a single array instead of a matrix with singleton dimensions, and min/max can't work on something like that.
Related
I have the locations of outer landmark points, something like this:
And now I want to "close" this contour from the outer chin and eyebrows and mask everything out to zero, so I want to obtain something like this :
So I guess I have 2 things to do :
Calculate the "outer" polygon, and create a mask out of it
Determine the inner region, multiply with binary mask, and crop from the bounding box
I'm not sure how to do either of them. Are there easy, MATLAB-specific ways for this? Or an example?
Thanks for any help!
Edit
I found roipoly from this page, but it asks the user to give the polygon I guess. Can I compute it automatically?
Edit 2
Yes I can, simply with BW = roipoly(img, c,r); img2 = img.*uint8(BW);
However, the problem still remains which landmarks to use for the contours. I can assign them rule based, but what if I want to select the "outer" landmarks automatically?
So I guess now the question is transformed into:
Given N points on 2D plane, how do I select the subset (i.e. the outer surface) that creates a polygon that covers all of the others?
You can use boundary and poly2mask as follows. I manually marked several points as you did not provide your face coordinates.
clc; clear all;
img = imread('FAm1z.jpg');
figure; imshow(img);
[x,y] = ginput(50);
k = boundary(x,y,0);
hold on;
plot(x,y,'ro');
plot(x(k),y(k));
mask = poly2mask(x(k), y(k), size(img,1), size(img,2));
mask(:,:,2) = mask;
mask(:,:,3) = mask(:,:,1);
roi = img;
roi(mask == 0) = 0;
figure; imshow(roi);
Output images:
I need to find similar corners in image (for example: 4 corners of a rectangle, same corners, just different orientation?).
I have this code:
% read the image into MATLAB and convert it to grayscale
I = imread('s2.jpg');
Igray = rgb2gray(I);
figure, imshow(I);
% We can see that the image is noisy. We will clean it up with a few
% morphological operations
Ibw = im2bw(Igray,graythresh(Igray));
se = strel('line',3,90);
cleanI = imdilate(~Ibw,se);
figure, imshow(cleanI);
% Perform a Hough Transform on the image
% The Hough Transform identifies lines in an image
[H,theta,rho] = hough(cleanI);
peaks = houghpeaks(H,10);
lines = houghlines(Ibw,theta,rho,peaks);
figure, imshow(cleanI)
% Highlight (by changing color) the lines found by MATLAB
hold on
After running this code I convert my starting image into a binary image with:
binary = im2bw(I);
after this I get a product from those 2 binary images and I think I get corners..
product = binary .* cleanI;
now I imfuse this picture with grayscale starting picture and get this:
I dont know what to do to get only those 4 corners!
Ok second try. Below a code that does not finally do the job but it might help.
Edge identifies the contours and with regionprops you get the characteristica of each identified element. As soon as you know what characteristics your desired object has you can filter it and plot it. I went through the Areas in shapedata.Area and the 6th largest was the one you were searching for. If you combine Area with some of the other charateristica you might get the one you want. As I said not ideal and final but perhaps a start ...
clear all
close all
source = imread('Mobile Phone.jpg');
im = rgb2gray(source);
bw = edge(im ,'canny',[],sqrt(2));
shapedata=regionprops (bwlabel(bw,8),'all');
%index = find([shapedata.Area]== max([shapedata.Area]));
index = 213;
data = shapedata(index).PixelList;
figure
imshow(im)
hold on
plot(data(:,1),data(:,2),'ro');
I have an image with very low contrast, from which I would like to extract a text object. Since it has a low contrast, I tried a few methods but no method gave me a satisfying result. I used watershed to extract the text object, but as the contrast is poor the extraction was not successful.
My program for watershed is:
I_cropped=imread(strcat('C:\Id\',currentfilename));
I_cropped = rgb2gray(I_cropped);
I_eq = histeq(I_cropped);
figure,imshow(I_eq);
bw = im2bw(I_eq, graythresh(I_eq));
bw2 = imfill(bw,'holes');
bw3 = imopen(bw2, ones(5,5));
bw4 = bwareaopen(bw3, 40);
bw = im2bw(I_eq, graythresh(I_eq));
figure,imshow(bw);
mask_em = imextendedmax(I_eq, 30);
mask_em = imclose(mask_em, ones(5,5));
mask_em = imfill(mask_em, 'holes');
mask_em = bwareaopen(mask_em, 40);
figure,imshow(mask_em);
I_eq_c = imcomplement(I_eq);
figure,imshow(I_eq_c);
I_mod = imimposemin(I_eq_c, ~bw4 | mask_em);
figure,imshow(I_mod);
L = watershed(I_mod);
figure,imshow(label2rgb(L));
I applied laplacian filter and to enhance edge, but it was not effective.
My objective is to extract text object. What method should I try for such low contrast image?
The image is attached:
Here is a way to do it.
First apply a median filter with a large kernel on the image to remove outliers and then apply a threshold to convert to a binary image. Note that playing with the kernel size of the filter alters the threshold level you need to use. Play around with it to see the output changing.
Then invert the image and apply regionprops to detect objects in the image. After that a little math to deduce the x and y origin (defining upper left corner) as well as the width and length of the large bounding box enclosing all the letters from the image.
Here is the code:
clear
clc
close all
Im = rgb2gray(imread('Imtext.png'));
%// Apply median filter to remove outliers
Im = medfilt2(Im,[9 9]);
%// Clear borders and apply threshold, then invert image
ImBW = imclearborder(~(im2bw(Im,.55)));
%// Find bounding boxes to delineate text regions.
S = regionprops(ImBW,'BoundingBox');
%// Concatenate all bounding boxes and obtain x,y, width and length of big
%// bounding box enclosing everything
bb = cat(1,S.BoundingBox);
LargeBB_x = min(bb(:,1));
LargeBB_y = min(bb(:,2));
LargeBB_height = max(bb(:,4));
%// Find last column in which pixel value is 1; that's the end
%// of the bounding box.
[~,ic] = find(ImBW==1);
MaxCol = max(ic(:));
LargeBB_width = MaxCol-LargeBB_x;
%// Display boxes
imshow(ImBW)
hold on
rectangle('Position',[LargeBB_x LargeBB_y LargeBB_width LargeBB_height],'EdgeColor','r','LineWidth',2)
hold off
And the output:
Or with the original image:
the region of interest in the image are calculated. Now how to embed the watermark into the roi..i ve put the code for embedding it in the whole image in lsb. How can it modified for roi alone?
clear all;
file_name='pout.tif';
cover_object=imread(file_name);
file_name='cameraman.tif';
message=imread(file_name);
message=double(message);
message=round(message./256);
message=uint8(message);
Mc=size(cover_object,1);
Nc=size(cover_object,2);
Mm=size(message,1);
Nm=size(message,2);
for ii = 1:Mc
for jj = 1:Nc
watermark(ii,jj)=message(mod(ii,Mm)+1,mod(jj,Nm)+1);
end
end
watermarked_image=cover_object;
for ii = 1:Mc
for jj = 1:Nc
watermarked_image(ii,jj)=bitset(watermarked_image(ii,jj),1,watermark(ii,jj));
end
end
imwrite(watermarked_image,'watermarkedimage','bmp');
figure(1)
imshow(watermarked_image,[])
title('Watermarked Image')
If your roi is rectangular, just loop over the appropriate subsection of the image rather than the whole thing.
If not, and you can define the watermarking as some function:
imgout = watermark(img1, img2);
Then you can use roifilt2 to apply that function just in your roi.
In this simple example,mask is a BW matrix where 1 indicates part of our roi (a mask can be created several different ways including using some of the interactive roi functions, see bottom section). img1 and img2 have already been loaded:
f = #(x) watermark(x,img2); % our very basic function
imgout = roifilt2(img1,mask,f);
Complications may arise if your img2 is smaller than img1 (or if you want to resize it to just cover the roi area). In this case, do everything on a subsection of img1/mask and then assemble the final image:
img1_s = img1(a:b,c:d); % these return the same size as img2
mask_s = mask(a:b,c:d);
imgout_s = roifilt2(img1_s,mask1_s,f);
imgout = img1;
imgout(a:b,c:d) = imgout_s;
A mask can be created several ways, some using the interactive roi functions, some not. A lot depends on how you have your roi in the first place - do you have a centre/radius, or do you want to hand-pick the location interactively, etc. Here are a few examples making use of the Image Processing Tool Box:
Interactive ROI function and createMask.
This will work for any of the interactive roi functions (imellipse, imfreehand, etc). You can interactively adjust the mask between the second and third steps. Do not close the image window before calling createMask.
h = imshow(img); %display image
e = imellipse(gca,[50 50 100 100]); % select roi
mask = createMask(e,h); % return mask
List of x/y points and poly2mask
If you have a list of points defining the outer edge of your roi, a quick non-interactive way of producing a mask is to use poly2mask. The third and fourth values define the total size of the mask returned.
mask = poly2mask(x,y,size(img,1),size(img,2));
Using morphological operators
strel defines a structuring element, and imdilate performs a dilation using that structuring element. Given an image which contains a single point, what that does is replace the point with the structuring element - in this case disk which will produce a circle (or as close as you can get using pixels). Other shapes (e.g. diamond) can be used with strel
mask = zeros(size(img));
mask(a,b) = 1; % a,b is the centre point of the circle
se = strel(disk,r,0); %r is the radius of the circle
mask = imdilate(mask,se);
I have a binary image that represents a number in MATLAB:
I'd like to fill all the digits. The desired result is:
The only thing I found was the imfill function, but that wasn't really helpfull since I've lost my inner data (the 9's inner circle for example).
Another possibility is to use the BWBOUNDARIES function, which:
traces the exterior boundaries of objects, as well as boundaries of
holes inside these objects
That information is contained in the fourth output A, an adjacency matrix that represents the parent-child-hole dependencies.
%# read binary image
bw = imread('SUvif.png');
%# find all boundaries
[B,L,N,A] = bwboundaries(bw, 8, 'holes');
%# exclude inner holes
[r,~] = find(A(:,N+1:end)); %# find inner boundaries that enclose stuff
[rr,~] = find(A(:,r)); %# stuff they enclose
idx = setdiff(1:numel(B), [r(:);rr(:)]); %# exclude both
bw2 = ismember(L,idx); %# filled image
%# compare results
subplot(311), imshow(bw), title('original')
subplot(312), imshow( imfill(bw,'holes') ), title('imfill')
subplot(313), imshow(bw2), title('bwboundaries')
The problem is how to distinguish the holes from the digits. A possible ad hoc solution is filtering them by the area of the pixels inside.
function SolveSoProblem()
I = imread('http://i.stack.imgur.com/SUvif.png');
%Fill all the holes
F = imfill(I,'holes');
%Find all the small ones,and mark their edges in the image
bw = bwlabel(I);
rp = regionprops(bw,'FilledArea','PixelIdxList');
indexesOfHoles = [rp.FilledArea]<150;
pixelsNotToFill = vertcat(rp(indexesOfHoles).PixelIdxList);
F(pixelsNotToFill) = 0;
figure;imshow(F);
%Remove the inner area
bw1 = bwlabel(F,4);
rp = regionprops(bw1,'FilledArea','PixelIdxList');
indexesOfHoles1 = [rp.FilledArea]<150;
pixelListToRemove = vertcat(rp(indexesOfHoles1).PixelIdxList);
F(pixelListToRemove) = 0;
figure;imshow(F);
end
After step(1):
After step(2):
Assuming the top-left pixel is always outside the regions to filled:
Work across the top line, copying pixels to the output image
When you come to a white pixel followed by a black pixel in the input image, start setting white pixels in the output image, until you come to black pixel followed by a white pixel.