Thick line intersection in colorbar - colorbar

I plot the Colorbar but, I have thick line intersection between Colorbar and bounds.
Do you have any idea for remove it or make it less thick?
bounds = [-0.9,-0.7,-0.5,-0.3,-0.1,0.1,0.3,0.5,0.7,0.9]
cbar = fig.colorbar(fig_SST, ax=axes, pad=0.04, ticks=bounds, location='bottom', shrink=0.5, orientation='horizontal')
cbar.ax.tick_params(labelsize=3)
cbar.set_label(label="[K/$\sigma$]", fontsize=3)

Related

Line removal from an image

I have used Hough Transforms to detect straight lines in an image. houghlines function returns line endpoints which one can plot. However i wish to remove the detected line from the image by creating a black line on the image of pixel width Two.
Here is a sample plot of Hough line onto the image.
This is a sample output. I want to set all the green pixels to zero. I have already tried using this Bresenham code to get all the points between two end points of the line and setting them to zero. However the results are not as expected.
There is different possible approaches to do so. A very simple approach is to use im2bw to set a threshold.
I = imread('fH7ha.jpg');
figure;
subplot 121; imshow(I); title('before')
I = rgb2gray(I);
I = im2bw(I, 0.9);
subplot 122; imshow(I); title('after')
The result is:
However this is a bit inaccurate since some part of the line are outside of your image, you can then isolate the green area and dilate it using imdilate:
I = imread('fH7ha.jpg');
figure;
subplot 131; imshow(I); title('before')
green_pixels = I(:,:,2)-I(:,:,1)-I(:,:,3);
green_pixels = im2bw(green_pixels, 0.1);
se = strel('disk', 2);
green_pixels = imdilate(green_pixels, se);
subplot 132; imshow(green_pixels); title('green pixels')
I = rgb2gray(I);
I(green_pixels) = 0;
subplot 133; imshow(I); title('after')

Changing the centre of transformation/mapping in polar coordinate MATLAB

After applying the following code I get output mapped image started from centre top point. How can I put the starting point to left bottom corner? So, the output image should be stretched from left bottom point(not from top centre as it is now)...
im = imread ('peppers.png');
im = rgb2gray(im);
[nZ,nX] = size(im);
theta = ((0:(nX-1))-nX/2)*(0.1*(pi/180)) - pi/2;
rr = (0:(nZ-1))*0.1e-3;
%% Plot image in rectangular coordinates
figure
imagesc(theta*(180/pi), rr*1e3, im)
xlabel('theta [deg]')
ylabel('r [mm]')
%% Create grids and convert polar coordinates to rectangular
[THETA,RR] = meshgrid(theta,rr);
[XX,YY] = pol2cart(THETA,RR);
%% Plot as surface, viewed from above
figure
surf(XX*1e3,YY*1e3,im,'edgecolor','none')
view(0,90)
xlabel('x [mm]')
ylabel('y [mm]')

How to crop rectangle box in image in matlab code

I want to crop a detected faces in my code. Here is my code.
function DisplayDetections(im, dets)
imshow(im);
k = size(dets,1);
hold on;
for i=1:k
rectangle('Position', dets(i,:),'LineWidth',2,'EdgeColor', 'r');
end
imcrop(rectangle);
hold off;
Their is syntax error in cropping.
Can anybody help in cropping rectangle box detected in above box.
That code only draws the rectangles in your image. If you actually want to crop out portions of the image with the defined rectangles, use imcrop.
As such, you would do something like this to store all of your cropped rectangles. This is assuming that im and dets are already defined in your code from your function:
k = size(dets,1);
cropped = cell(1,k);
for i=1:k
cropped{k} = imcrop(im, dets(i,:));
end
cropped would be a cell array where each element will store a cropped image defined by each rectangle within your dets array. This is assuming that dets is a 2D array where there are 4 columns, and the number of rows determines how many rectangles you have. Each row of dets should be structured like:
[xmin ymin width height]
xmin, ymin are the horizontal and vertical co-ordinate of the top-left corner of the rectangle, and width and height are the width and height of the rectangle.
If you want to access a cropped portion in the cell array, simply do:
crp = cropped{k};
k would be the kth rectangle detected in your image.

creating an image in matlab of 9 small white circles in a large black square

Im looking to create an image in Matlab of a large black rectangle with 9 small circles arranged as a a 3x3 array aligned in the centre of the rectangle, i.e. the centre circle will have its midpoint in the centre of the square.
I need the circles evenly spaced apart with some distance between each circle and between the outer circles and the border of the rectangle (think of a square piece of paper with 9 holes placed in it by stabbing it with a pen). I need this so that i can see how image convolution using a 2D gaussian will distort this image.
However I’m relatively new to Matlab and have been trying to create this image. I have successfully made a black/white square and a white circle in a black square which takes up most of the square itself but I cant seem to make a small white circle in any desired location in a black square let alone multiple small circles in a specific alignment.
This is what I have used to create the black square with a large circle:
X = ones([100,1])*([-50:49]);
Y = ([-50:49]')*(ones([1,100]));
Z = (X.^2)+(Y.^2);
image = zeros([100 100]);
image(find(Z<=50^2)) = 1;
imshow(image)
If I understood correctly, try this:
% size of each small box. Final image will be 3Nx3N
N = 100;
% create a circle mask
t = linspace(0,2*pi,50); % approximated by 100 lines
r = (N-10)/2; % circles will be separated by a 10 pixels border
circle = poly2mask(r*cos(t)+N/2+0.5, r*sin(t)+N/2+0.5, N, N);
% replicate to build image
img = repmat(circle, 3,3);
subplot(121), imshow(img)
% after applying Gaussian filter
h = fspecial('gaussian', [15 15], 2.5);
img2 = imfilter(im2double(img), h);
subplot(122), imshow(img2)

Matlab: how to avoid ellipses overlapping in image?

I've been using a function file [ret]=drawellipse(x,y,a,b,angle,steps,color,img). Calling the function through a script file to draw random ellipses in image. But once i set the random center point(x,y), and random a, b, there is high possibility that the ellipses intersection would occur. How can i prevent the intersection? (I'm supposed to draw the ellipses that are all separate from each other)
Well, over here i have a function file which is to check whether the ellipses got overlap or not,overlap = overlap_ellipses(x0,y0,a0,b0,angle0,x1,y1,a1,b1,angle1). If the two ellipses are overlap, then the 'overlap=1', otherwise 'overlap=0'.
Based on all these, i tested in the command window:
x=rand(4,1)*400; % x and y are the random coodinates for the center of ellipses
y=rand(4,1)*400;
a=[50 69 30 60]; % major axis for a and b, i intend to use random also in the future
b=[20 40 10 40]; % minor axis
angle=[30 90 45 0]; % angle of ellipse
steps=10000;
color=[255 0 0]; % inputs for another function file to draw the ellipse
img=zeros(500,500,3);
The following i want to dispaly the ellipses if overlap==0, and 'if overlap==1', decrease the a and b, till there is no intersection. Lastly, to imshow the img.
for i=1:length(x)
img=drawellipse(x(i),y(i),a(i),b(i),angle(i),steps,color,img);
end
For me now, i have difficulty in coding the middle part. How can i use the if statement to get the value of overlap and how to make the index corresponding to the ellipse i need to draw.
i tested a bit like
for k=1:(length(x)-1)
overlap = overlap_ellipses(x(1),y(1),a(1),b(1),angle(1),x(1+k),y(1+k),a(1+k),b(1+k),angle(1+k))
end
it returns
overlap=0
overlap=0
overlap=1
it is not [0 0 1]. I can't figure it out, thus stuck in the process.
The final image shoule look like the picture in this voronoi diagram of ellipses.
(There is no intersection between any two ellipses)
Assuming you are drawing the ellipses into a raster graphics image, you could calculate the pixels you would have to draw for an ellipse, check whether these pixels in the image are still of the background color, and draw the ellipse only if the answer is yes, otherwise reject it (because something else, i.e. another ellipse, is in the way) and try other x,y,a and b.
Alternatively, you could split your image into rectangles (not neccessarily of equal size) and place one ellipse in each of those, picking x,y,a,b such that no ellipse exceeds its rectangle - then the ellipses cannot overlap either, but it depends on how much "randomness" your ellipse placing should have whether this suffices.
The mathematically rigorous way would be to store x,y,a,b of each drawn ellipse and for each new ellipse, do pairwise checks with each of those whether they have common points by solving a system of two quadratic equations. However, this might be a bit complicated, especially once the angle is not 0.
Edit in response to the added code: Instead of fixing all x's and y's before the loop, you can determine them inside the loop. Since you know how many ellipses you want, but not how many you have to sample, you need a while loop. The test loop you give may come in handy, but you need to compare all previous ellipses to the one created in the loop iteration, not the first one.
i=1;
while (i<=4) %# or length(a), or, more elegantly, some pre-defined max
x(i) = rand*400; y(i) = rand*400; %# or take x and y as givren and decrease a and b
%# now, check overlap for given center
overlap = false;
for k=1:(i-1)
overlap = overlap || overlap_ellipses(x(i),y(i),a(i),b(i),angle(i),x(k),y(k),a(k),b(k),angle(k))
end
if (~overlap)
img = drawellipse(x(i),y(i),a(i),b(i),angle(i),steps,color,img);
i = i+1; %# determine next ellipse
end %# else x(i) and y(i) will be overwritten in next while loop iteration
end
Of course, if a and b are fixed, it may happen that no ellipse fits the image dimensions if the already present ones are unfortunately placed, resulting in an infinite loop.
Regarding your plan of leaving the center fixed and decreasing the ellipse's size until it fits: where does your overlap_ellipses method come from? Maybe itcan be adapted to return a factor by which one ellipse needs to be shrinked to fit next to the other (and 1 if it fits already)?
The solution proposed by #arne.b (the first one) is a good way to rasterize non-overlapping ellipses.
Let me illustrate that idea with an example. I will be extending my previous answer:
%# color image
I = imread('pears.png');
sz = size(I);
%# parameters of ellipses
num = 7;
h = zeros(1,num);
clr = lines(num); %# color of each ellipse
x = rand(num,1) .* sz(2); %# center x-coords
y = rand(num,1) .* sz(1); %# center y-coords
a = rand(num,1) .* 200; %# major axis length
b = rand(num,1) .* 200; %# minor axis length
angle = rand(num,1) .* 360; %# angle of rotation
%# label image, used to hold rasterized ellipses
BW = zeros(sz(1),sz(2));
%# randomly place ellipses one-at-a-time, skip if overlaps previous ones
figure, imshow(I)
axis on, hold on
for i=1:num
%# ellipse we would like to draw directly on image matrix
[ex,ey] = calculateEllipse(x(i),y(i), a(i),b(i), angle(i), 100);
%# lets plot the ellipse (overlayed)
h(i) = plot(ex,ey, 'LineWidth',2, 'Color',clr(i,:));
%# create mask for image pixels inside the ellipse polygon
mask = poly2mask(ex,ey,sz(1),sz(2));
%# get the perimter of this mask
mask = bwperim(mask,8);
%# skip if there is an existing overlapping ellipse
if any( BW(mask)~=0 ), continue, end
%# use the mask to place the ellipse in the label image
BW(mask) = i;
end
hold off
legend(h, cellstr(num2str((1:num)','Line%d')), 'Location','BestOutside') %'
%# set pixels corresponding to ellipses using specified colors
clr = im2uint8(clr);
II = I;
for i=1:num
BW_ind = bsxfun(#plus, find(BW==i), prod(sz(1:2)).*(0:2));
II(BW_ind) = repmat(clr(i,:), [size(BW_ind,1) 1]);
end
figure, imshow(II, 'InitialMagnification',100, 'Border','tight')
Note how the overlap test is performed in the order the ellipses are added, thus after Line1 (blue) and Line2 (green) are drawn, Line3 (red) will be skipped because it overlaps one of the previous ones, and so on for the rest...
One option is to keep track of all the ellipses already drawn, and to make sure the next set of [x,y,a,b] does not produce a new ellipse which intersects with the existing ones. You can either invoke random numbers until you come up with a set that fulfills the condition, or once you have a set which violates the condition, decrease the values of a and/or b until no intersection occurs.

Resources