I'm building a Matlab GUI which let the user use some interactive tools for image processing (a single mfile with no fig file),
like rotation of the image with imrotate and angle parameter and clearing out pixels with bwareaopen and area parameter.
At first, I was thinking of plotting the tools with my mfile with somehthing like
function myGUI( grayI )
h_fig = figure;
h_ax = imshow( grayI );
title('Drag line and press button to rotate image');
ImgSize = size(grayI);
h_lev = imline(gca, [ 0.2*ImgSize(2), 0.1*ImgSize(1); ...
0.8*ImgSize(2) 0.1*ImgSize(1) ] );
h_lev.addNewPositionCallback( #LineUpdateFcn );
% text for the angle
h_txt = uicontrol('Style','text','String','Angle = []', 'unit', 'norm', ...
'pos',[0 0.9 .1 .05]);
%add rotate button
h_btn = uicontrol('unit','norm','pos',[0 0.95 .1 .05]);
set(h_btn,'string','Rotate','callback',#RotateImageWithLever);
% save the elements data in figure
setappdata(h_fig,'h_lev',h_lev);
setappdata(h_fig,'h_ax',h_ax);
setappdata(h_fig,'h_txt',h_txt);
% wait for user to close figure
waitfor( h_fig );
but then I came across the design of toolbar as built-in class in FileExchange Fireworks and thought maybe I'm missing the right-and-neat way to design my toolbar with classes and built in uitoolbar command.
Any advice on designing my toolbar from single mfile?
Using a custom toolbar would probably be a good solution for the tool you describe. Create the toolbar using UITOOLBAR and add push or toggle buttons using UIPUSHTOOL and UITOOGLETOOL, respectively. This can readily be done in the initialization stage of your GUI m-file.
I've given a simple example below. Some caveats:
The logic of the toggle button vs. push button is not implemented correctly since inverting the image, flipping it and then inverting again will not give the correct result. However, I am just trying to show how to code toolbar buttons not how to process images.
You will probably want to use more creative icons then what I've given in the CData property.
I encourage a object-oriented approach to this solution despite my procedural example.
Here it is:
function myGUI(grayI)
persistent grayICopy;
%# Keep a persistent copy of the image data to be used in the toolbar tool
%# callbacks. Other possibilities here are to not store this data
%# persistently and instead read it from the plotted values or restructure
%# this whole code as a class and store the raw image data in a class
%# property.
grayICopy = grayI;
%# Create the figure window and show the image.
hFigure = figure;
hAxes = axes('Parent', hFigure);
image(grayI, 'Parent', hAxes); %# I don't have the Image processing Toolbox
%# Create toolbar
hToolbar = uitoolbar('Parent', hFigure);
%# Add a toolbar button for 90deg clockwise rotation
uipushtool('Parent', hToolbar, ...
'ClickedCallback', #flipVertical, ...
'CData', ...
repmat([0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; ...
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0; ...
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0; ...
0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0; ...
0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0; ...
0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0; ...
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], [1 1 3]));
%# Add a toolbar toggle button for inverting image
uitoggletool('Parent', hToolbar, ...
'OnCallback', #toggleInverseOn, ...
'OffCallback', #toggleInverseOff, ...
'CData', ...
repmat([0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; ...
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0; ...
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0; ...
0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0; ...
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0; ...
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0; ...
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0], [1 1 3]));
function flipVertical(src, eventdata)
grayICopy = flipdim(grayICopy, 1);
image(grayICopy, 'Parent', hAxes);
end
function toggleInverseOn(src, eventdata)
image(1-grayICopy, 'Parent', hAxes);
end
function toggleInverseOff(src, eventdata)
image(grayICopy, 'Parent', hAxes);
end
end
Related
In the the matlab documentation there is this sentence about closing an image with imclose:
The morphological close operation is a dilation followed by an
erosion, using the same structuring element for both operations
I tried it out but the result is not the same. Can sombody tell me how I can close an image only with imdilate and imerode?
I = [0 0 0 0 0 0 0 0;
0 0 1 1 1 0 0 0;
0 0 0 1 1 1 0 0;
0 1 1 1 0 0 1 0;
0 0 1 0 0 1 1 0;
0 1 0 0 1 0 1 0;
0 0 1 0 1 0 1 0;
0 0 0 0 0 0 0 0];
Q = [0 1 0; 0 1 0; 0 1 1];
Q_n = [1 1 0; 0 1 0; 0 1 0];
J = imclose(I,Q)
D = imdilate(I,Q);
S = imerode(D,Q)
Closed result:
0 0 0 0 0 0 0 0
0 0 1 1 1 0 0 0
0 0 1 1 1 1 0 0
0 1 1 1 1 1 1 0
0 1 1 1 1 1 1 0
0 1 0 0 1 1 1 0
0 0 1 0 1 1 1 0
0 0 0 0 0 0 0 0
dilate and then erode:
0 0 1 1 1 0 0 0
0 0 1 1 1 0 0 0
0 0 1 1 1 1 0 0
0 1 1 1 1 1 1 0
0 1 1 1 1 1 1 0
0 1 0 0 1 1 1 1
0 0 1 0 1 1 1 1
0 0 1 0 1 1 1 1
I am trying to apply an imrotate to an image however the angle of the image is unknown and changes with each image.
If i have converted the image to binary would it be possible to find the lowest y position '1' and the leftmost'1' and use the gradient/angle between them as the angle for my image rotate?
for example:
binary positions diagram
Using the angle between these two positions and aligning it with the x axis?
Current Progress - converted to binary and made the edges more distinguishable:
% convert to binary
greyImage = rgb2gray(C); % greyscale
cannyImage = edge(greyImage, 'canny'); % canny edge detection
% fill the gaps in the shape
se = strel('disk',2);
bw = imclose(cannyImage, se);
filled = imfill(bw, 'holes');
imshow(filled);
[~,lowerMostCol] = max(cumsum(sum(filled,2)));
[~,leftMostRow] = max(sum(filled,1)==1);
Approach #1
With a as the binary image, you could do something like this -
[~,lowermost] = max(cumsum(sum(a,2)));
lowermostpt = [lowermost,find(a(lowermost,:),1,'first')]
[~,rightmost] = max(cumsum(sum(a,1)));
rightmostpt = [find(a(:,rightmost),1,'first'),rightmost]
[~,topmost] = max(sum(a,2)==1);
topmostpt = [topmost,find(a(topmost,:),1,'first')]
[~,leftmost] = max(sum(a,1)==1);
leftmostpt = [find(a(:,leftmost),1,'first'),leftmost]
For performance efficiency, it might be a good idea to store the summations once and re-use later on.
Sample run -
a =
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 1 1 1 1 1 0 0 0
0 0 0 0 0 0 1 1 1 1 1 1 1 0 0
0 0 0 0 0 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 1 1 1 1 1 1 1 0 0
0 0 0 0 0 0 0 1 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
lowermostpt =
12 10
rightmostpt =
8 14
topmostpt =
4 10
leftmostpt =
8 6
Approach #2 Using bwboundaries from Image-processing toolbox -
idx = cell2mat(bwboundaries(a))
[~,p1] = min(idx(:,1))
topmostpt = idx(p1,:)
[~,p2] = max(idx(:,1))
lowermostpt = idx(p2,:)
[~,p3] = min(idx(:,2))
leftmostpt = idx(p3,:)
[~,p4] = max(idx(:,2))
rightmostpt = idx(p4,:)
I have a matrix looks like:
0 0 0 0 0
1 0 0 0 0
0 2 0 0 0
0 0 2 0 0
0 0 0 1 0
1 0 0 0 1
0 4 0 0 0
0 0 3 0 0
6 0 0 4 0
0 3 0 0 2
0 0 5 0 0
It is 11x5 matrix.
I want to interpolate between the values vertically for each column.
Any help ?
Thanks.
M =[0 0 0 0 0
1 0 0 0 0
0 2 0 0 0
0 0 2 0 0
0 0 0 1 0
1 0 0 0 1
0 4 0 0 0
0 0 3 0 0
6 0 0 4 0
0 3 0 0 2
0 0 5 0 0];
xi = 1:size(M,1)
for colIdx = 1:size(M,2)
col = M(:,colIdx);
x = xi(~~col); %// Note that ~~col is a logical vector of elements that are not equal to zero. i.e. it's the same as col ~= 0
y = col(~~col);
M(:,colIdx) = interp1(x,y,xi);
end
then if you want the outer points to be 0 add this line after the loop:
M(isnan(M)) = 0;
I need to work with imfill in Matlab (Version 2010b, 7.11.0). I now think there is a bug in the program.
The most simple example that i found here is following: (Fills the Image background (0) beginning at the position [4 3])
BW = [ 0 0 0 0 0 0 0 0;
0 1 1 1 1 1 0 0;
0 1 0 0 0 1 0 0;
0 1 0 0 0 1 0 0;
0 1 0 0 0 1 0 0;
0 1 1 1 1 0 0 0;
0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0];
imfill(BW,[4 3])
According to the specifications this should work IMHO, but I always get following message. Can anyone tell me what I am doing wrong?
??? Error using ==> iptcheckconn at 56
Function IMFILL expected its second input argument, CONN,
to be a valid connectivity specifier.
A nonscalar connectivity specifier must be 3-by-3-by- ...
-by-3.
Error in ==> imfill>parse_inputs at 259
iptcheckconn(conn, mfilename, 'CONN', conn_position);
Error in ==> imfill at 124
[I,locations,conn,do_fillholes] = parse_inputs(varargin{:});
Error in ==> test at 9
imfill(BW,[4 3])
That does not explain the problem but converting BW to a logical array does work. I'm not sure as to why it's like this though:
clear
close all
clc
BW = [ 0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 0
0 1 0 0 0 1 0 0
0 1 0 0 0 1 0 0
0 1 0 0 0 1 0 0
0 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
BW2 = imfill(logical(BW),[4 3])
BW2 =
0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 0
0 1 1 1 1 1 0 0
0 1 1 1 1 1 0 0
0 1 1 1 1 1 0 0
0 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
As you have already seen in the other solution by #Benoit_11, that most probably that input wasn't of logical class, which was throwing an error at you. So, you are set there!
Now, I would like to put forth a tiny bit of bonus suggestion here.
Let's suppose you have a set of seed points with their row and column IDs and you would like to fill an image with those seed points in one go. For that case,
you need to use those IDs as column vectors. Thus, if you have the row and column IDs as -
row_id = [4 3];
col_id = [3 7];
You can fill image with this -
BW = imfill(BW,[row_id(:) col_id(:)])
But, the following code would throw error at you -
BW = imfill(BW,[row_id col_id])
I'm using Matlab. I have a 2-D Binary image/array. like this
0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 1 1 1 0 0 1 0 0
0 0 1 1 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
I want to find out center of very first white block/Circle with respect to y-axis
Answer of the above image will be.
0 1 0
1 1 1
0 1 0
Anyone who have have a simplest solution for this.
If you are looking for exact matches of the template, you can use a moving filter, one example is:
H=[0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 1 0 0 0 0 0 0;
0 0 0 0 0 1 1 1 0 0 1 0 0;
0 0 1 1 0 0 1 0 0 0 0 0 0;
0 0 0 1 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 1 0 0 0 0 0 0;
0 0 0 0 0 0 1 1 0 0 0 0 0;
0 0 0 0 0 0 1 0 0 0 0 0 0];
b=[0 1 0;
1 1 1;
0 1 0];
C=filter2(b,H, 'same');
[x,y]=find(C==max(max(C)));
x and y are the locations of your template in the order that it appears from the top left corner of your array.
Edit: if you have the Image Processing Toolbox and are looking for a less strict way of finding objects that have a roughly circular shape you can use regionprops with the 'Centroid' and 'Eccentricity' arguments with the bwconncomp function.
ObjectStats=regionprops(bwconncomp(H,4), 'Centroid', 'Eccentricity');
Objects with an 'Eccentricity' of 0 (or close to 0) will be the circles.
idx=find(cell2mat({ObjectStats.Eccentricity})==0); % Change ==0 to <0.2 or something to make it less strict.
ctrs={ObjectStats.Centroid};
>> ctrs{1,idx(1)}
ans =
7 3
Note that in your case, a lone pixel is an object with an eccentricity of 0, it is the smallest 'circle' that you can find. If you need to define a minimum size, use the 'Area' property of regionprops
You can do this with a simple 2 dimensional convolution. It will "overlay" the filter along a larger matrix and multiply the filter by the values it is overlaying. If the product is equal to the sum of the filter, then you know you found a match.
Here is some simple code.
mat = [0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 1 1 1 0 0 1 0 0
0 0 1 1 0 0 1 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 1 1 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0];
filt = [0 1 0
1 1 1
0 1 0];
[row,col] = find(conv2(mat,filt,'same') == sum(filt(:)))