Related
I have a black and white image as shown below:
I want to separate the white components of this image and then save them as separate image. This image has four white parts. I want to separate them and save four new images; each containing a white part of the image.
To achieve this, I wrote the following code:
BW=imread('img11_Inp.jpg');
imshow(BW);
BW=imbinarize(BW);
[L, num] = bwlabel(BW);
for k = 1 : num
thisBlob = ismember(L, k);
h = int2str(k);
filname = strcat(h,'_Out.jpg');
imwrite(thisBlob,filname);
figure
imshow(thisBlob, []);
end
Problem
This code separates the white parts and saves them but the size of the white part saved in the new image is same as in the original image. See the output images below:
Output images
Desired Output images
I want the output images to contain increased size of the white part of the original image. Following images are the ones I want:
Question
How can I modify the above code so that I can get the desired output images ?
Steps:
Find the boundary of the white portion.
To include the black portion, subtract a constant from the top left corner.If it is less than or equal to zero, it means we have reached or exceeded the left corner of the actual image, so set it 1. If it is greater than zero then all is fine.
Make similar adjustments for the right bottom corner.
Crop to the desired size.
Code:
%Finding the boundary of the white
[~, c1] = find(thisBlob, 1); [~, r1] = find(thisBlob.', 1);
[~, c2] = find(thisBlob, 1, 'last'); [~, r2] = find(thisBlob.', 1, 'last');
%Making adjustments to include the black portion
k = 10; %constant defining max number of black pixels
mxlim = size(X); %to be used to confirm that we don't exceed the boundary of the image
r1 = r1-10; r1(r1<=0)=1; c1 = c1-10; c1(c1<=0)=1;
r2 = r2+10; r2(r2>mxlims(1)) = mxlim(1); c2 = c2+10; c2(c2>mxlim(2)) = mxlims(2);
%Extracting the desired portion
thisBlob = thisBlob(r1:r2, c1:c2);
Output for the provided images:
You can change the number of black pixels by changing the constant k in the code.
Test Case when the white portion is on the Edge:
To verify if it also works if the white portion is on the edge like this image:
The code gives the following output for the above image:
Actually, what you want to perform is a crop with a little bit of span around the object. This can be easily achieved using imcrop that you must call providing the rectangle you want to keep.
In order to identify the rectangle:
Find the minimum an maximum rows that contain a white pixel (y-axis);
Find the minimum an maximum columns that contain a white pixel (x-axis);
Calculate width and height of the rectangle using maximum - minimum.
Since you want to crop using a little margin (in my example I set its value to 10 but you have full control over it), you must subtract that margin to minimum values and adding it to maximum values, but paying attention not to go out of the boundaries of the image (that's where the little min-max game comes into play).
Here is the full working code:
img = imread('img11_Inp.jpg');
imshow(img);
img_bin = imbinarize(img);
[lab,num] = bwlabel(img_bin);
span = 10;
for k = 1:num
file = [num2str(k) '_Out.jpg'];
blob = ismember(lab,k);
blob_size = size(blob);
col_idx = find(any(blob == true,1));
x1 = max([1 (min(col_idx) - span)]);
x2 = min([blob_size(2) (max(col_idx) + span)]);
width = x2 - x1;
row_idx = find(any(blob == true,2));
y1 = max([1 (min(row_idx) - span)]);
y2 = min([blob_size(1) (max(row_idx) + span)]);
height = y2 - y1;
blob_crop = imcrop(blob,[x1 y1 width height]);
imwrite(blob_crop,file);
figure();
imshow(blob_crop,[]);
end
Also, don't use int2str(k) in order to obtain a string representation of your index. Your index is actually a double so you are forcing a double (no pun intended) cast: double -> int and then int -> char array. Just use num2str.
Result:
I have 2 images ("before" and "after"). I would like to show a final image where the left half is taken from the before image and the right half is taken from the after image.
The images should be separated by a white diagonal line of predefined width (2 or 3 pixels), where the diagonal is specified either by a certain angle or by 2 start and end coordinates. The diagonal should overwrite a part of the final image such that the size is the same as the sources'.
Example:
I know it can be done by looping over all pixels to recombine and create the final image, but is there an efficient way, or better yet, a built-in function that can do this?
Unfortunately I don't believe there is a built-in solution to your problem, but I've developed some code to help you do this but it will unfortunately require the image processing toolbox to play nicely with the code. As mentioned in your comments, you have this already so we should be fine.
The logic behind this is relatively simple. We will assume that your before and after pictures are the same size and also share the same number of channels. The first part is to declare a blank image and we draw a straight line down the middle of a certain thickness. The intricacy behind this is to declare an image that is slightly bigger than the original size of the image. The reason why is because I'm going to draw a line down the middle, then rotate this blank image by a certain angle to achieve the first part of what you desire. I'll be using imrotate to rotate an image by any angle you desire. The first instinct is to declare an image that's the same size as either the originals, draw a line down the middle and rotate it. However, if you do this you'll end up with the line being disconnected and not draw from the top to the bottom of the image. That makes sense because the line being drawn on an angle covers more pixels than if you were to draw this vertically.
Using Pythagorean's theorem, we know that the longest line that can ever be drawn on your image is the diagonal. Therefore we declare an image that is sqrt(rows*rows + cols*cols) in both the rows and columns where rows and cols are the rows and columns of the original image. After, we'll take the ceiling to make sure we've covered as much as possible and we add a bit of extra room to accommodate for the width of the line. We draw a line on this image, rotate it then we'll crop the image after so that it's the same size as the input images. This ensures that the line drawn at whatever angle you wish is fully drawn from top to bottom.
That logic is the hardest part. Once you do that, you declare two logical masks where you use imfill to fill the left side of the mask as one mask and we'll invert the mask to find the other mask. You will also need to use the line image that we created earlier with imrotate to index into the masks and set the values to false so that we ignore these pixels that are on the line.
Finally, you take each mask, index into your image and copy over each portion of the image you desire. You finally use the line image to index into the output and set the values to white.
Without further ado, here's the code:
% Load some example data
load mandrill;
% im is the image before
% im2 is the image after
% Before image is a colour image
im = im2uint8(ind2rgb(X, map));
% After image is a grayscale image
im2 = rgb2gray(im);
im2 = cat(3, im2, im2, im2);
% Declare line image
rows = size(im, 1); cols = size(im, 2);
width = 5;
m = ceil(sqrt(rows*rows + cols*cols + width*width));
ln = false([m m]);
mhalf = floor(m / 2); % Find halfway point width wise and draw the line
ln(:,mhalf - floor(width/2) : mhalf + floor(width/2)) = true;
% Rotate the line image
ang = 20; % 20 degrees
lnrotate = imrotate(ln, ang, 'crop');
% Crop the image so that it's the same dimensions as the originals
mrowstart = mhalf - floor(rows/2);
mcolstart = mhalf - floor(cols/2);
lnfinal = lnrotate(mrowstart : mrowstart + rows - 1, mcolstart : mcolstart + cols - 1);
% Make the masks
mask1 = imfill(lnfinal, [1 1]);
mask2 = ~mask1;
mask1(lnfinal) = false;
mask2(lnfinal) = false;
% Make sure the masks have as many channels as the original
mask1 = repmat(mask1, [1 1 size(im,3)]);
mask2 = repmat(mask2, [1 1 size(im,3)]);
% Do the same for the line
lnfinal = repmat(lnfinal, [1 1 size(im, 3)]);
% Specify output image
out = zeros(size(im), class(im));
out(mask1) = im(mask1);
out(mask2) = im2(mask2);
out(lnfinal) = 255;
% Show the image
figure;
imshow(out);
We get:
If you want the line to go in the other direction, simply make the angle ang negative. In the example script above, I've made the angle 20 degrees counter-clockwise (i.e. positive). To reproduce the example you gave, specify -20 degrees instead. I now get this image:
Here's a solution using polygons:
function q44310306
% Load some image:
I = imread('peppers.png');
B = rgb2gray(I);
lt = I; rt = B;
% Specify the boundaries of the white line:
width = 2; % [px]
offset = 13; % [px]
sz = size(I);
wlb = [floor(sz(2)/2)-offset+[0,width]; ceil(sz(2)/2)+offset-[width,0]];
% [top-left, top-right; bottom-left, bottom-right]
% Configure two polygons:
leftPoly = struct('x',[1 wlb(1,2) wlb(2,2) 1], 'y',[1 1 sz(1) sz(1)]);
rightPoly = struct('x',[sz(2) wlb(1,1) wlb(2,1) sz(2)],'y',[1 1 sz(1) sz(1)]);
% Define a helper grid:
[XX,YY] = meshgrid(1:sz(2),1:sz(1));
rt(inpolygon(XX,YY,leftPoly.x,leftPoly.y)) = intmin('uint8');
lt(repmat(inpolygon(XX,YY,rightPoly.x,rightPoly.y),1,1,3)) = intmin('uint8');
rt(inpolygon(XX,YY,leftPoly.x,leftPoly.y) & ...
inpolygon(XX,YY,rightPoly.x,rightPoly.y)) = intmax('uint8');
final = bsxfun(#plus,lt,rt);
% Plot:
figure(); imshow(final);
The result:
One solution:
im1 = imread('peppers.png');
im2 = repmat(rgb2gray(im1),1,1,3);
imgsplitter(im1,im2,80) %imgsplitter(image1,image2,angle [0-100])
function imgsplitter(im1,im2,p)
s1 = size(im1,1); s2 = size(im1,2);
pix = floor(p*size(im1,2)/100);
val = abs(pix -(s2-pix));
dia = imresize(tril(ones(s1)),[s1 val]);
len = min(abs([0-pix,s2-pix]));
if p>50
ind = [ones(s1,len) fliplr(~dia) zeros(s1,len)];
else
ind = [ones(s1,len) dia zeros(s1,len)];
end
ind = uint8(ind);
imshow(ind.*im1+uint8(~ind).*im2)
hold on
plot([pix,s2-pix],[0,s1],'w','LineWidth',1)
end
OUTPUT:
I have a binary image:
and I'm trying to see if a 100x150 rectangle will fit in any blank space on the map.
I tried creating a rectangular strel & then eroding & dilating the picture to get rid of any areas smaller than needed:
se = strel('rectangle',[150, 100]);
BW = imerode(BW,se);
BW = imdilate(BW,se);
Unfortunately, it finds a hole prematurely
which is only 80x150. I think the erosion is failing since it's against the wall & only needs half the width, but don't know how to fix it.
Also, if I'm headed down the wrong path, please feel free to set me straight. Ultimately, I just need to find the upper-left corner of the a blank space at least as big as 100x150.
The approach below works quite well and runs fairly quickly. It has a few nested loops but you can probably further optimize the performance, I mainly just wanted to get it working for you. Keep in mind though that if you comment out the fprintf() and the plotting command, that will speed things up.
I downloaded your image from your Stack post but I believe that my downloaded version has a different size (398x398) than the raw data you are working with, so keep that in mind when viewing my results below.
As indicated in the code, you supply the width (w) and height (h), the algorithm then returns all of the (col, row) positions where the rectangle can fit.
Side Note: I believe this provides a solution to a 2D version of the Bin packing problem, but I'm not sure about that, you can check out the link above if thats of interest to you.
Either way, its a great example of computational problem where an exhaustive search can be a carried out rather quickly.
To verify the results, I added simple plotting of the rectangles. Keep in mind that if the rectangle fits in more than one position, a plot of the multiple rectangles begins to look rather jumbled as they are drawn repeatedly on top of one another (with offsets).
As an example case where only a single rectangle is found, I use: w = 29; h = 102; and then the result shows that the only position where this particular rectangle can fit, has the upper left corner = (row = 295, col = 368) (this rectangle size will likely only work for my downloaded version of your data):
In summary, I first I load the data and then convert to a binary map (0's and 1's):
% Note: '0' = black; '1' = white
data = round(im2double(rgb2gray(imread(filepath))));
figure(1);imshow(data); set(gcf,'Color',[1 1 1]);
hold on;
Input the search width and height:
w = 29;
h = 102;
sze = size(data);
numRows = sze(1);
numCols = sze(2);
Next we just do a search to see what will fit at each row and col position:
for col = 1:numCols - w - 1
for row = 1:numRows - h - 1
doesFit = fitshere(data, row,col, w, h);
if (doesFit == 1)
fprintf('row = %d; col = %d \n',row,col);
colX = [col col+w col+w col col];
colY = [row row row+h row+h row];
line(colX,colY,'Color','r','linewidth',2);
end
end
end
hold off;
You will need the following function to check if a given rectangle can fit in the array:
function [val] = fitshere(data, row, col, w, h)
val = 1;
for i = col:col + w
for j = row:row + h
if (data(j,i) == 0) % if this is true, we are in the black!
val = 0;
return;
end
end
end
return;
If your interested in knowing if your rectangle will fit at all (say either width X height or height X width), you can simply repeat the search after swapping the width and height.
Hope this helps.
lets do this with some matlab idioms
M=binaryImage;
sz=size(M);
nrows = 100;
ncols = 150 ;
colsum = cumsum(M,1);
cols_are_good = colsum(nrows+1:end,:)-colsum(1:end-nrows+1,:)==0;
% nrows empty rows below this point. in this column
rows_are_also_good = cols_are_good(:,ncols+1:end)-cols_are_good(:,1:end+1-ncols)==0;
and Bob's your uncle, that last variable contains 1 in all places that have nrows below them clear and each of those has ncols to the side
Hi I am trying to get the boundary orientation of an image from the image gradient or canny edge detector as in equation 11 of http://www.cs.swan.ac.uk/~csjason/papers/xxmm-pami2008.pdf
I currently have:
clear all
Img = imread('littlecircle.png');
Img = Img(:,:,1);
Img = double(Img);
w = size(Img,1); % width size
h = size(Img,2); % height size
[Ix,Iy] = gradient(Img); %gradient of image
i=1; %iteration for magnetic field loop
b=0; %initialize b to zero
% Magnetic Field
for pxRow = 1:h % fixed pixel row
for pxCol = 1:w % fixed pixel column
for r = 1:h % row of distant pixel
for c = 1:w % column of distant pixel
O(c,r) = [-Iy(c,r),Ix(c,r)]; % O(x) = (-1).^lambda(-Iy(x),Ix(x)) --ERROR HERE
end
end
B(i) = {O}; % filling a cell array with results. read below
i = i+1;
end
end
However I am getting a subscript indices mismatch when storing into O(c,r). Why is this? and also if anyone thinks there is a better way to do this from the paper then I would love to here it. Thanks.
You could do the canny + orientation detection in one step, by modifying matlab's canny edge detection code or modify an alternative like this. Canny works by determining the orientation on each step, so you could modify the canny code to also return an orientation map for each pixel.
I have an image in MATLAB:
im = rgb2gray(imread('some_image.jpg');
% normalize the image to be between 0 and 1
im = im/max(max(im));
And I've done some processing that resulted in a number of points that I want to highlight:
points = some_processing(im);
Where points is a matrix the same size as im with ones in the interesting points.
Now I want to draw a circle on the image in all the places where points is 1.
Is there any function in MATLAB that does this? The best I can come up with is:
[x_p, y_p] = find (points);
[x, y] = meshgrid(1:size(im,1), 1:size(im,2))
r = 5;
circles = zeros(size(im));
for k = 1:length(x_p)
circles = circles + (floor((x - x_p(k)).^2 + (y - y_p(k)).^2) == r);
end
% normalize circles
circles = circles/max(max(circles));
output = im + circles;
imshow(output)
This seems more than somewhat inelegant. Is there a way to draw circles similar to the line function?
You could use the normal PLOT command with a circular marker point:
[x_p,y_p] = find(points);
imshow(im); %# Display your image
hold on; %# Add subsequent plots to the image
plot(y_p,x_p,'o'); %# NOTE: x_p and y_p are switched (see note below)!
hold off; %# Any subsequent plotting will overwrite the image!
You can also adjust these other properties of the plot marker: MarkerEdgeColor, MarkerFaceColor, MarkerSize.
If you then want to save the new image with the markers plotted on it, you can look at this answer I gave to a question about maintaining image dimensions when saving images from figures.
NOTE: When plotting image data with IMSHOW (or IMAGE, etc.), the normal interpretation of rows and columns essentially becomes flipped. Normally the first dimension of data (i.e. rows) is thought of as the data that would lie on the x-axis, and is probably why you use x_p as the first set of values returned by the FIND function. However, IMSHOW displays the first dimension of the image data along the y-axis, so the first value returned by FIND ends up being the y-coordinate value in this case.
This file by Zhenhai Wang from Matlab Central's File Exchange does the trick.
%----------------------------------------------------------------
% H=CIRCLE(CENTER,RADIUS,NOP,STYLE)
% This routine draws a circle with center defined as
% a vector CENTER, radius as a scaler RADIS. NOP is
% the number of points on the circle. As to STYLE,
% use it the same way as you use the rountine PLOT.
% Since the handle of the object is returned, you
% use routine SET to get the best result.
%
% Usage Examples,
%
% circle([1,3],3,1000,':');
% circle([2,4],2,1000,'--');
%
% Zhenhai Wang <zhenhai#ieee.org>
% Version 1.00
% December, 2002
%----------------------------------------------------------------
Funny! There are 6 answers here, none give the obvious solution: the rectangle function.
From the documentation:
Draw a circle by setting the Curvature property to [1 1]. Draw the circle so that it fills the rectangular area between the points (2,4) and (4,6). The Position property defines the smallest rectangle that contains the circle.
pos = [2 4 2 2];
rectangle('Position',pos,'Curvature',[1 1])
axis equal
So in your case:
imshow(im)
hold on
[y, x] = find(points);
for ii=1:length(x)
pos = [x(ii),y(ii)];
pos = [pos-0.5,1,1];
rectangle('position',pos,'curvature',[1 1])
end
As opposed to the accepted answer, these circles will scale with the image, you can zoom in an they will always mark the whole pixel.
Hmm I had to re-switch them in this call:
k = convhull(x,y);
figure;
imshow(image); %# Display your image
hold on; %# Add subsequent plots to the image
plot(x,y,'o'); %# NOTE: x_p and y_p are switched (see note below)!
hold off; %# Any subsequent plotting will overwrite the image!
In reply to the comments:
x and y are created using the following code:
temp_hull = stats_single_object(k).ConvexHull;
for k2 = 1:length(temp_hull)
i = i+1;
[x(i,1)] = temp_hull(k2,1);
[y(i,1)] = temp_hull(k2,2);
end;
it might be that the ConvexHull is the other way around and therefore the plot is different. Or that I made a mistake and it should be
[x(i,1)] = temp_hull(k2,2);
[y(i,1)] = temp_hull(k2,1);
However the documentation is not clear about which colum = x OR y:
Quote: "Each row of the matrix contains the x- and y-coordinates of one vertex of the polygon. "
I read this as x is the first column and y is the second colum.
In newer versions of MATLAB (I have 2013b) the Computer Vision System Toolbox contains the vision.ShapeInserter System object which can be used to draw shapes on images. Here is an example of drawing yellow circles from the documentation:
yellow = uint8([255 255 0]); %// [R G B]; class of yellow must match class of I
shapeInserter = vision.ShapeInserter('Shape','Circles','BorderColor','Custom','CustomBorderColor',yellow);
I = imread('cameraman.tif');
circles = int32([30 30 20; 80 80 25]); %// [x1 y1 radius1;x2 y2 radius2]
RGB = repmat(I,[1,1,3]); %// convert I to an RGB image
J = step(shapeInserter, RGB, circles);
imshow(J);
With MATLAB and Image Processing Toolbox R2012a or newer, you can use the viscircles function to easily overlay circles over an image. Here is an example:
% Plot 5 circles at random locations
X = rand(5,1);
Y = rand(5,1);
% Keep the radius 0.1 for all of them
R = 0.1*ones(5,1);
% Make them blue
viscircles([X,Y],R,'EdgeColor','b');
Also, check out the imfindcircles function which implements the Hough circular transform. The online documentation for both functions (links above) have examples that show how to find circles in an image and how to display the detected circles over the image.
For example:
% Read the image into the workspace and display it.
A = imread('coins.png');
imshow(A)
% Find all the circles with radius r such that 15 ≤ r ≤ 30.
[centers, radii, metric] = imfindcircles(A,[15 30]);
% Retain the five strongest circles according to the metric values.
centersStrong5 = centers(1:5,:);
radiiStrong5 = radii(1:5);
metricStrong5 = metric(1:5);
% Draw the five strongest circle perimeters.
viscircles(centersStrong5, radiiStrong5,'EdgeColor','b');
Here's the method I think you need:
[x_p, y_p] = find (points);
% convert the subscripts to indicies, but transposed into a row vector
a = sub2ind(size(im), x_p, y_p)';
% assign all the values in the image that correspond to the points to a value of zero
im([a]) = 0;
% show the new image
imshow(im)