Hello,
I'm trying to find a position and make a rectangle in a maximum mean position in a image. But i don't know how the position is found and how i handle them. Here is a portion of macro I wrote to measure "Mean" and "Centroid(X,Y)" in the rectangle. -- run("Blobs (25K)");//an example run("Set Measurements...", "mean centroid display redirect=None decimal=3"); imagewidth = getWidth(); imageheight = getHeight(); x_pixel = imagewidth;//size of the image y_pixel = imageheight;//size of the image length = 200;//size of rectangle for (x=0; x < x_pixel-length; x++){ for (y=0; y < y_pixel-length; y++){ makeRectangle(x, y, length, length); run("Measure"); }} -- Then, I wanna get X and Y value showing maximum mean. In this case, row# 3012 is the maximum mean. So, X(maximum_mean) = 155; Y(maximum_mean) = 141. Finally, I gonna add a following code and draw a rectangle. -- makeRectangle(X(maximum_mean), Y(maximum_mean), length, length); -- Does anyone know how I have to? Thanks in advance. Tom |
Hi Tom,
doing tens of thousands of measurements will be rather slow. I would propose to use the Fast Filters to blur the image with a rectangular kernel, then determine the maximum. Then, the kernel size size will be 2*radius+1, which is always an odd number like 199 or 201, but that should not hurt too much, I guess. In the macro below "Maximum" is defined as brightest pixel. If you don't like this, invert the LUT if the image has an inverting LUT. The macro uses the 'Find Maxima' command with very large tolerance; this finds the highest maximum if there is one. If there is no maximum (all values equal), the macro will fail because there will be no results. You may check for this separately. If the maximum is extended over several pixels, 'Find Maxima' will select the pixel that is closest to the centroid of these pixels. radius = 19; //rectangle width&height = 2*radius+1 run("Blobs (25K)"); run("Duplicate...", " "); run("32-bit") run("Fast Filters", "filter=[border-limited mean] x=&radius y=&radius preprocessing=none"); run("Clear Results"); run("Find Maxima...", "noise=1e99 output=List"); x = getResult("X",0); y = getResult("Y",0); v = getPixel(x,y); close(); makeRectangle(x-radius, y-radius, 2*radius+1, 2*radius+1); print("highest mean value=",v); The 'blobs' image is quite small for rectangles of 200x200 pixels, so the example is for a radius of 19 pixels, i.e., rectangles of 39x39 pixels. With 199*199 pixels (r=99), you will get an edge maximum, and you should expect this: At the edges, the mean is calculated over fewer pixels than in the middle, so the values of the mean over large areas will scatter more. Michael ________________________________________________________________ On Feb 25, 2015, at 14:37, Tom Kon wrote: > Hello, > > I'm trying to find a position and make a rectangle in a maximum mean > position in a image. > But i don't know how the position is found and how i handle them. > Here is a portion of macro I wrote to measure "Mean" and "Centroid(X,Y)" in > the rectangle. > > -- > run("Blobs (25K)");//an example > run("Set Measurements...", "mean centroid display redirect=None decimal=3"); > imagewidth = getWidth(); > imageheight = getHeight(); > x_pixel = imagewidth;//size of the image > y_pixel = imageheight;//size of the image > > length = 200;//size of rectangle > for (x=0; x < x_pixel-length; x++){ > for (y=0; y < y_pixel-length; y++){ > makeRectangle(x, y, length, length); > run("Measure"); > }} > -- > > Then, I wanna get X and Y value showing maximum mean. In this case, row# > 3012 is the maximum mean. So, X(maximum_mean) = 155; Y(maximum_mean) = 141. > Finally, I gonna add a following code and draw a rectangle. > -- > makeRectangle(X(maximum_mean), Y(maximum_mean), length, length); > -- > > Does anyone know how I have to? > Thanks in advance. > > Tom > > > > -- > View this message in context: http://imagej.1557.x6.nabble.com/Find-a-max-position-tp5011762.html > Sent from the ImageJ mailing list archive at Nabble.com. > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |