Ploting A square around minima of a matrix
Show older comments
if we have minimum value of a matrix with the help of function min(mat). How we can plot a square around that minima of matrix. i would be very grateful if i got my answer as soon as possible
1 Comment
Azzi Abdelmalek
on 4 Nov 2012
Edited: Azzi Abdelmalek
on 4 Nov 2012
Dimension of your square?
Answers (3)
Azzi Abdelmalek
on 4 Nov 2012
a=1; % square dimension
mat=rand(5)
[c,idx]=min(mat(:))
[ii,jj]=ind2sub([5 5],idx)
x=[ii-a/2 ii+a/2 ii+a/2 ii-a/2 ii-a/2];
y=[jj-a/2 jj-a/2 jj+a/2 jj+a/2 jj-a/2];
plot(x,y,'r');hold on
plot(ii,jj,'o')
axis([ii-a ii+a jj-a jj+a])
x = rand(100,1);
y = rand(100,1)
[idx idx] = min(y);
plot(x,y,'.')
hold on;
plot(x(idx),y(idx),'s','MarkerSize',20) %Or whatever size you want
Image Analyst
on 4 Nov 2012
Since you put image processing in your tags, I think you're looking for an image-based solution, so I offer up this demo. It generates a small sample image, then finds the min value and the (row,column) coordinates where the min(s) occur(s) (there may be more than 1 location where the min occurs for an integer image). Then for every location, it draws a box around that pixel, right at the interface/edge between one pixel and its neighbors. Just copy, paste, and run and you'll see.
clc;
close all;
clearvars;
% Generate a small sample image and display it (enlarged).
grayImage = randi(255, [30, 40])
imshow(grayImage, [], 'InitialMagnification', 800);
hold on;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Find the min value.
[minValue, minLinearIndex] = min(grayImage(:))
% Find out the rows and columns where it occurs.
% There may be more than 1!!!
[minRows minCols] = find(grayImage == minValue)
% For every min pixel found, plot a box around it.
lineWidth = 2;
for p = 1 : length(minRows)
% Get the coordinates at the EDGES of the pixels.
y1 = minRows(p)-0.5;
y2 = minRows(p) + 0.5;
x1 = minCols(p) - 0.5;
x2 = minCols(p) + 0.5;
% Get the vertices of a closed box around this point.
% This will be 5 coordinates if the box is to be closed.
boxX = [x1 x2 x2 x1 x1];
boxY = [y1 y1 y2 y2 y1];
% Now finally plot the box around this pixel.
plot(boxX, boxY, 'r-', 'LineWidth', lineWidth);
end
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!