Connect disjoint edges in binary image - image

I performed some operations on an image of a cube and I obtained a binary image of the edges of the cube which are disconnected at some places.The image I obtained is shown below:
I want to join the sides to make it a closed figure.I have tried the following:
BW = im2bw(image,0.5);
BW = imdilate(BW,strel('square',5));
figure,imshow(BW);
But this only thickens the image.It does not connect the edges.I have also tried bwmorph() and various other functions, but it is not working.Can anyone please suggest any function or steps to connect the edges? Thank you

This could be one approach -
%// Read in the input image
img = im2bw(imread('http://i.imgur.com/Bl7zhcn.jpg'));
%// There seems to be white border, which seems to be non-intended and
%// therefore could be removed
img = img(5:end-4,5:end-4);
%// Thin input binary image, find the endpoints in it and connect them
im1 = bwmorph(img,'thin',Inf);
[x,y] = find(bwmorph(im1,'endpoints'));
for iter = 1:numel(x)-1
two_pts = [y(iter) x(iter) y(iter+1) x(iter+1)];
shapeInserter = vision.ShapeInserter('Shape', 'Lines', 'BorderColor', 'White');
rectangle = int32(two_pts);
im1 = step(shapeInserter, im1, rectangle);
end
figure,imshow(im1),title('Thinned endpoints connected image')
%// Dilate the output image a bit
se = strel('diamond', 1);
im2 = imdilate(im1,se);
figure,imshow(im2),title('Dilated Thinned endpoints connected image')
%// Get a convex shaped blob from the endpoints connected and dilate image
im3 = bwconvhull(im2,'objects',4);
figure,imshow(im3),title('Convex blob corresponding to previous output')
%// Detect the boundary of the convex shaped blob and
%// "attach" to the input image to get the final output
im4 = bwboundaries(im3);
idx = im4{:};
im5 = false(size(im3));
im5(sub2ind(size(im5),idx(:,1),idx(:,2))) = 1;
img_out = img;
img_out(im5==1 & img==0)=1;
figure,imshow(img_out),title('Final output')
Debug images -

I used the above code to write the following one.I haven't tested it on many images and it may not be as efficient as the one above but it executes faster comparatively.So I thought I would post it as a solution.
I = imread('image.jpg'); % your original image
I=im2bw(I);
figure,imshow(I)
I= I(5:end-4,5:end-4);
im1 = bwmorph(I,'thin',Inf);
[x,y] = find(bwmorph(im1,'endpoints'));
for iter = 1:numel(x)-1
im1=linept(im1, x(iter), y(iter), x(iter+1), y(iter+1));
end
im2=imfill(im1,'holes');
figure,imshow(im2);
BW = edge(im2);
figure,imshow(BW);
se = strel('diamond', 1);
im3 = imdilate(BW,se);
figure,imshow(im3);
The final result is this:
I got the "linept" function from here:http://in.mathworks.com/matlabcentral/fileexchange/4177-connect-two-pixels

Related

ROI is written in lighter colors than original picture

I'm trying to locate an object (here a PWB) on a picture.
First I do this by finding the largest contour. Then I want to rewrite solely this object into a new picture so that in the future I can work on smaller pictures.
The problem however is that when I rewrite this ROI, the picture gets of a lighter color than the original one.
CODE:
Original = cv2.imread(picture_location)
image = cv2.imread(mask_location)
img = cv2.medianBlur(image,29)
imgray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
dst = cv2.bitwise_and(Original, image)
roi = cv2.add(dst, Original)
ret,thresh = cv2.threshold(imgray,127,255,0)
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
area = 0
max_x = 0
max_y = 0
min_x = Original.shape[1]
min_y = Original.shape[0]
for i in contours:
new_area = cv2.contourArea(i)
if new_area > area:
area = new_area
cnt = i
x,y,w,h = cv2.boundingRect(cnt)
min_x = min(x, min_x)
min_y = min(y, min_y)
max_x = max(x+w, max_x)
max_y = max(y+h, max_y)
roi = roi[min_y-10:max_y+10, min_x-10:max_x+10]
Original = cv2.rectangle(Original,(x-10,y-10),(x+w+10,y+h+10),(0,255,0),2)
#Writing down the images
cv2.imwrite('Pictures/PCB1/LocatedPCB.jpg', roi)
cv2.imwrite('Pictures/PCB1/LocatedPCBContour.jpg',Original)
Since I don't have 10 reputation yet I cannot post the pictures. I can however provide the links:
Original
Region of Interest
The main question is how do I get the software to write down the ROI in the exact same colour as the original picture?
I'm a elektromechanical engineer however, so I'm fairly new to this, remarks on the way I wrote my code would also be appreciated if possible.
The problem is that you first let roi = cv2.add(dst, Original)
and finally cut from the lighten picture in here:
roi = roi[min_y-10:max_y+10, min_x-10:max_x+10]
If you want to crop the original image, you should do:
roi = Original[min_y-10:max_y+10, min_x-10:max_x+10]
You can perhaps perform an edge detection after blurring your image.
How to select best parameters for Canny edge? SEE HERE
lower = 46
upper = 93
edged = cv2.Canny(img, lower, upper) #--- Perform canny edge on the blurred image
kernel = np.ones((5,5),np.uint8)
dilate = cv2.morphologyEx(edged, cv2.MORPH_DILATE, kernel, 3) #---Morphological dilation
_, contours , _= cv2.findContours(dilate, cv2.RETR_EXTERNAL, 1) #---Finds all parent contours, does not find child contours(i.e; does not consider contours within another contour)
max = 0
cc = 0
for i in range(len(contours)): #---For loop for finding contour with maximum area
if (cv2.contourArea(contours[i]) > max):
max = cv2.contourArea(contours[i])
cc = i
cv2.drawContours(img, contours[cc], -1, (0,255,0), 2) #---Draw contour having the maximum area
cv2.imshow(Contour of PCB.',img)
x,y,w,h = cv2.boundingRect(cnt[cc]) #---Calibrates a straight rectangle for the contour of max. area
crop_img = img1[y:y+h, x:x+w] #--- Cropping the ROI having the coordinates of the bounding rectangle
cv2.imshow('cropped PCB.jpg',crop_img)

Color only a segment of an image in Matlab

I'm trying to color only a segment of an image in Matlab. For example, I load an RGB image, then I obtain a mask with Otsu's method (graythresh). I want to keep the color only in the pixels that have value of 1 after applying im2bw with graythresh as the threshold. For example:
image = imread('peppers.png');
thr = graythresh(image);
bw = im2bw(image, thr);
With this code I obtain the following binary image:
My goal is to keep the color in the white pixels.
Thanks!
I have another suggestion on how to replace the pixels we don't care about. This works by creating linear indices for each of the slices where black pixels exist in the bw image. The summation with the result of find is done because bw is the size of just one "slice" of image and this is how we get the indices for the other 2 slices.
Starting MATLAB 2016b:
image(find(~bw)+[0 numel(bw)*[1 2]]) = NaN;
In older versions:
image(bsxfun(#plus,find(~bw),[0 numel(bw)*[1 2]])) = NaN;
Then imshow(image) gives:
Note that NaN gets converted to 0 for integer classes.
Following the clarification that the other pixels should be kept in their gray version, see the below code:
% Load image:
img = imread('peppers.png');
% Create a grayscale version:
grayimg = rgb2gray(img);
% Segment image:
if ~verLessThan('matlab','9.0') && exist('imbinarize.m','file') == 2
% R2016a onward:
bw = imbinarize(grayimg);
% Alternatively, work on just one of the color channels, e.g. red:
% bw = imbinarize(img(:,:,1));
else
% Before R2016a:
thr = graythresh(grayimg);
bw = im2bw(grayimg, thr);
end
output_img = repmat(grayimg,[1 1 3]);
colorpix = bsxfun(#plus,find(bw),[0 numel(bw)*[1 2]]);
output_img(colorpix) = img(colorpix);
figure; imshow(output_img);
The result when binarizing using only the red channel:
Your question misses "and replace the rest with black". here are two ways:
A compact solution: use bsxfun:
newImage = bsxfun(#times, Image, cast(bw, 'like', Image));
Although I am glad with the previous one, you can also take a look at this step-by-step approach:
% separate the RGB layers:
R = image(:,:,1);
G = image(:,:,2);
B = image(:,:,3);
% change the values to zero or your desired color wherever bw is false:
R(~bw) = 0;
G(~bw) = 0;
B(~bw) = 0;
% concatenate the results:
newImage = cat(3, R, G, B);
Which can give you different replacements for the black region:
UPDATE:
According to the comments, the false area of bw should be replaced with grayscale image of the same input. This is how to achieve it:
image = imread('peppers.png');
thr = graythresh(image);
bw = im2bw(image, thr);
gr = rgb2gray(image); % generate grayscale image from RGB
newImage(repmat(~bw, 1, 1, 3)) = repmat(gr(~bw), 1, 1, 3); % substitude values
% figure; imshow(newImage)
With this result:

Plot over an image background in MATLAB

I'd like to plot a graph over an image. I followed this tutorial to Plot over an image background in MATLAB and it works fine:
% replace with an image of your choice
img = imread('myimage.png');
% set the range of the axes
% The image will be stretched to this.
min_x = 0;
max_x = 8;
min_y = 0;
max_y = 6;
% make data to plot - just a line.
x = min_x:max_x;
y = (6/8)*x;
imagesc([min_x max_x], [min_y max_y], img);
hold on;
plot(x,y,'b-*','linewidth',1.5);
But when I apply the procedure to my study case, it doesn't work. I'd like to do something like:
I = imread('img_png.png'); % here I load the image
DEM = GRIDobj('srtm_bigtujunga30m_utm11.tif');
FD = FLOWobj(DEM,'preprocess','c');
S = STREAMobj(FD,flowacc(FD)>1000);
% with the last 3 lines I calculated the stream network on a geographic area using the TopoToolBox
imagesc(I);
hold on
plot(S)
The aim is to plot the stream network over the satellite image of the same area.
The only difference between the two examples that doesn't let the code working is in the plot line, in the first case "plot(x,y)" works, in the other one "plot(S)" doesn't.
Thanks guys.
This is the satellite image, imagesc(I)
It is possible that the plot method of the STREAMobj performs it's own custom plotting including creating new figures, axes, toggling hold states, etc. Because you can't easily control what their plot routine does, it's likely easier to flip the order of your plotting so that you plot your stuff after the toolbox plots the STREAMobj. This way you have completely control over how your image is added.
% Plot the STREAMobj
hlines = plot(S);
% Make sure we plot on the same axes
hax = ancestor(hlines, 'axes');
% Make sure that we can add more plot objects
hold(hax, 'on')
% Plot your image data on the same axes
imagesc(I, 'Parent', hax)
Maybe I am preaching to the choir or overlooking something here but the example you used actually mapped the image to the data range of the plot, hence the lines:
% set the range of the axes
% The image will be stretched to this.
min_x = 0;
max_x = 8;
min_y = 0;
max_y = 6;
imagesc([min_x max_x], [min_y max_y], img);
where you directly plot your image
imagesc(I);
If now your data coordinates and your image coordinates are vastly different you either see one or the other.
Thanks guys, I solved in this way:
I = imread('orto.png'); % satellite image loading
DEM = GRIDobj('demF1.tif');
FD = FLOWobj(DEM,'preprocess','c');
S = STREAMobj(FD,flowacc(FD)>1000); % Stream network extraction
x = S.x; % [node attribute] x-coordinate vector
y = S.y; % [node attribute] y-coordinate vector
min_x = min(x);
max_x = max(x);
min_y = min(y);
max_y = max(y);
imagesc([min_x max_x], [min_y max_y], I);
hold on
plot(S);
Here's the resulting image: stream network over the satellite image
Actually the stream network doesn't match the satellite image just because I'm temporarily using different images and DEM.

automatically finding length of object in binary image (MATLAB)

I have the following binary image:
http://www.4shared.com/download/BozvHQcHba/untitled2.jpg?lgfp=3000
I manually select the start and end points using the ruler in imtool to get the length. Is there a way to automatically get the length i.e the first white pixel to last white pixel (longest length) without doing it manually.
Code
%%// Get the binary image
img = imread(filename1);
BW = im2bw(img);
%%// Find biggest blob
[L,num] = bwlabel( BW );
count_pixels_per_obj = sum(bsxfun(#eq,L(:),1:num));
[~,ind] = max(count_pixels_per_obj);
biggest_blob = (L==ind);
%%// Find row and column info for all edge pixels
BW1 = edge(biggest_blob,'canny');
[row1,col1] = find(BW1);
%%// Get the distance matrix and thus find the largest length separating
%%// them which is the length of the object/blob
%dist_mat = pdist2([row1 col1],[row1 col1]);
dist_mat = dist2s([row1 col1],[row1 col1]); %// If you do not have pdist2
length_blob = max(dist_mat(:))
Associated function
function out = dist2s(pt1,pt2)
out = NaN(size(pt1,1),size(pt2,1));
for m = 1:size(pt1,1)
for n = 1:size(pt2,1)
if(m~=n)
out(m,n) = sqrt( (pt1(m,1)-pt2(n,1)).^2 + (pt1(m,2)-pt2(n,2)).^2 );
end
end
end

Playing image sequences in MATLAB

Greetings to all of you.
I have this somewhat frustrating problem, and I hope that you kindly help me solve it.
I am developing a human tracking system in MATLAB, and would like to show the result in an appealing GUI (also in MATLAB using GUIDE).
There is this main window where an image sequence of about 2500 gray scale images of size 320x240 would be played like a video but where the humans be outlined in them nicely.
The challenge is; these images need a bit of processing (detection outlining of humans) before being shown on the window.
Now, is it possible to display a set of images while at the same time do some processing for another set to be shown afterwards?
I would very much prefer it to play like a normal video, but I guess that would be somehow ambitious.
Here is an example showing a scenario similar to what you described. This was adapted from the demo I mentioned in the comments.
function ImgSeqDemo()
figure()
for i=1:10
%# read image
img = imread( sprintf('AT3_1m4_%02d.tif',i) );
%# process image to extract some object of interest
[BW,rect] = detectLargestCell(img);
%# show image
imshow(img), hold on
%# overlay mask in red color showing object
RGB = cat(3, BW.*255, zeros(size(BW),'uint8'), zeros(size(BW),'uint8'));
hImg = imshow(RGB); set(hImg, 'AlphaData',0.5);
%# show bounding rectangle
rectangle('Position', rect, 'EdgeColor','g');
hold off
drawnow
end
end
Here is the processing function used above. In your case, you would insert your algorithm instead:
function [BW,rect] = detectLargestCell(I)
%# OUTPUT
%# BW binary mask of largest detected cell
%# rect bounding box of largest detected cell
%# find components
[~, threshold] = edge(I, 'sobel');
BW = edge(I,'sobel', threshold*0.5);
se90 = strel('line', 3, 90);
se0 = strel('line', 3, 0);
BW = imdilate(BW, [se90 se0]);
BW = imclearborder(BW, 4);
BW = bwareaopen(BW, 200);
BW = bwmorph(BW, 'close');
BW = imfill(BW, 'holes');
%# keep largest component
CC = bwconncomp(BW);
stats = regionprops(CC, {'Area','BoundingBox'});
[~,idx] = max([stats.Area]);
rect = stats(idx).BoundingBox;
BW(:) = 0;
BW(CC.PixelIdxList{idx}) = 1;
end

Resources