Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
826 views
in Technique[技术] by (71.8m points)

image - Axis coordinates to pixel coordinates? (Matlab)

How can i convert the axis coordinates into pixel coordinates? I have a set of data which include the negative and floating values, i need to put all of the data into the image. But the pixel coordinates are all positive integer. how to solve the negative issue?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

From what I understand, you have a set of points representing an ellipse, and you would to draw them directly inside an image matrix (not just display them on screen).

For this, you can use the POLY2MASK function to convert the ellipse into a binary mask. Then by computing its perimeter, this will give us a binary mask representing only the pixels that constitute the ellipse, which is applied to the image to set the color of the pixels.

Consider the example below. I am using the function calculateEllipse.m from a previous question here on SO:

%# some image
I = imread('pout.tif');
sz = size(I);

%# ellipse we would like to draw directly on image matrix
[x,y] = calculateEllipse(100,50, 150,50, 30, 100);

%# lets show the image, and plot the ellipse (overlayed).
%# note how ellipse have floating point coordinates, 
%# and also have points outside the image boundary
figure, imshow(I)
hold on, plot(x,y, 'LineWidth',2)
axis([-50 250 -50 300]), axis on

%# create mask for image pixels inside the ellipse polygon
BW = poly2mask(x,y,sz(1),sz(2));

%# get the perimter of this mask
BW = bwperim(BW,8);

%# use the mask to index into image
II = I;
II(BW) = 255;
figure, imshow(II)

ellipse_drawn_on_screen ellipse_drawn_on_image

This should give you superior results to simply rounding the coordinates of x and y (plus it handles out-of-boundary points for us). Make sure to read the algorithm section of POLY2MASK to see how it works on a subpixel level.


EDIT:

if you are working with an RGB image (3D matrix), the same applies, you only need to change the last part where we use the binary mask :

%# color of the ellipse (red)
clr = [255 0 0];                %# assuming UINT8 image data type

%# use the mask to index into image
II = I;
z = false(size(BW));
II( cat(3,BW,z,z) ) = clr(1);   %# R channel
II( cat(3,z,BW,z) ) = clr(2);   %# G channel
II( cat(3,z,z,BW) ) = clr(3);   %# B channel
figure, imshow(II)

Here is yet another way:

%# use the mask to index into image
II = I;
BW_ind = bsxfun(@plus, find(BW), prod(sz(1:2)).*(0:2));
II(BW_ind) = repmat(clr, [size(BW_ind,1) 1]);
figure, imshow(II)

pillsetc.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...