Can't drag 'imrect' object for high resolution images - matlab

I'm making a GUI (with GUIDE) in which there is an axis used to display image sequences. In order to let the user select a region of interest in the sequence I'm using 'imrect'. The problem is the following: everything goes fine when images are smaller than 512x512 pixels (approximately), however for larger images (I tried 600x600 and 1024x1024) the rectangle does appear, I can change its size but I can't drag it around. I though it had to be with axis units so I changed the property from 'pixels' to 'normalized' and use normalized coordinates, but it does not work.
Here is my code to create the rectangle and restrain its movement to the axis limits:
hROI = imrect(hVideo,[Width/4 Height/4 Width/2 Height/2]; % Arbitrary size and position of the rectangle, centered on the image.
fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim'));
setPositionConstraintFcn(hROI,fcn);
When I perform the same operation on those large images outside the GUI it works. Any hint is welcome!
thanks

I found a workaround to the problem, in case it can help someone:
In the call to imshow just before calling imrect, we need to specify the axis limits as the "XData" and "YData" parameters.
Example:
imshow(Movie{Frame},'parent',handles.axes1_Video,'XData',get(gca,'XLim'),'YData',get(gca,'YLim'))
It works for images up to 1024x1024.

Related

Export image at full resolution with axes in MATLAB

I am trying to save a large image (1641x6139) at full resolution with MATLAB with axes. Doing this without axes is easy using the imwrite command. To plot the image with axes, I write:
image(x,y,my_image);
Because the image is very large, MATLAB automatically reduces the resolution of the image. The window size would have to be significantly larger than the screen to display the entire image at full resolution. I have tried a couple different approaches to exporting the figure and each runs into the same problem: the image is pixelated at a coarser resolution than the original image. The problem is not that the new image has fewer pixels; instead, neighboring pixels have been assigned the same value, creating the appearance of coarser-scale pixelation.
For example, where H is the figure handle, I tried:
set(H, 'PaperPosition', [0, 0, width , height ])
print -dpng -r500 my_filename
Increasing width, height, or resolution all had the effect of increasing the number of pixels in the image, but the apparent pixelation is unchanged.
I also tried using the function export_fig, which seems to be designed for this type of problem, claiming to export figures with contained images in their native resolution. The resulting image appears to have the correct number of pixels, but as before the problem is that neighboring pixels have the same value, creating the appearance of coarser pixelation absent from the original image. The code I used is:
export_fig(my_filename,'-a1','-native')
Here are a couple images showing roughly the same zoomed in portion of my image:
The first is the original image and the second is the image resulting from export_fig. The second looks pixelated, although again note that each image contains roughly the same number of actual pixels.
Any advice/solutions would be much appreciated! This problem has been much more frustrating than I expected.
I figured out a simple solution. First, I create a blank image of the correct size, add the axes, and then use export_fig to generate an image of the axes. Then, I write my full-resolution image into the image matrix, and save the resulting image using imwrite.

Matlab: separate connected components

I was working on my image processing problem with detecting coins.
I have some images like this one here:
and wanted to separate the falsely connected coins.
We already tried the watershed method as stated on the MATLAB-Homepage:
the-watershed-transform-strategies-for-image-segmentation.html
especially since the first example is exactly our problem.
But instead we get a somehow very messed up separation as you can see here:
We already extracted the area of the coin using the regionprops Extrema parameter and casting the watershed only on the needed area.
I'd appreciate any help with the problem or even another method of getting it separated.
If you have the Image Processing Toolbox, I can also suggest the Circular Hough Transform through imfindcircles. However, this requires at least version R2012a, so if you don't have it, this won't work.
For the sake of completeness, I'll assume you have it. This is a good method if you want to leave the image untouched. If you don't know what the Hough Transform is, it is a method for finding straight lines in an image. The circular Hough Transform is a special case that aims to find circles in the image.
The added advantage of the circular Hough Transform is that it is able to detect partial circles in an image. This means that those regions in your image that are connected, we can detect them as separate circles. How you'd call imfindcircles is in the following fashion:
[centers,radii] = imfindcircles(A, radiusRange);
A would be your binary image of objects, and radiusRange is a two-element array that specifies the minimum and maximum radii of the circles you want to detect in your image. The outputs are:
centers: A N x 2 array that tells you the (x,y) co-ordinates of each centre of a circle that is detected in the image - x being the column and y being the row.
radii: For each corresponding centre detected, this also gives the radius of each circle detected. This is a N x 1 array.
There are additional parameters to imfindcircles that you may find useful, such as the Sensitivity. A higher sensitivity means that it is able to detect circular shapes that are more non-uniform, such as what you are showing in your image. They aren't perfect circles, but they are round shapes. The default sensitivity is 0.85. I set it to 0.9 to get good results. Also, playing around with your image, I found that the radii ranged from 50 pixels to 150 pixels. Therefore, I did this:
im = im2bw(imread('http://dennlinger.bplaced.net/t06-4.jpg'));
[centers,radii] = imfindcircles(im, [50 150], 'Sensitivity', 0.9);
The first line of code reads in your image directly from StackOverflow. I also convert this to logical or true black and white as the image you uploaded is of type uint8. This image is stored in im. Next, we call imfindcircles in the method that we described.
Now, if we want to visualize the detected circles, simply use imshow to show your image, then use the viscircles to draw the circles in the image.
imshow(im);
viscircles(centers, radii, 'DrawBackgroundCircle', false);
viscircles by default draws the circles with a white background over the contour. I want to disable this because your image has white circles and I don't want to show false contouring. This is what I get with the above code:
Therefore, what you can take away from this is the centers and radii variables. centers will give you the centre of each detected circle while radii will tell you what the radii is for each circle.
Now, if you want to simulate what regionprops is doing, we can iterate through all of the detected circles and physically draw them onto a 2D map where each circle would be labeled by an ID number. As such, we can do something like this:
[X,Y] = meshgrid(1:size(im,2), 1:size(im,1));
IDs = zeros(size(im));
for idx = 1 : numel(radii)
r = radii(idx);
cen = centers(idx,:);
loc = (X - cen(1)).^2 + (Y - cen(2)).^2 <= r^2;
IDs(loc) = idx;
end
We first define a rectangular grid of points using meshgrid and initialize an IDs array of all zeroes that is the same size as the image. Next, for each pair of radii and centres for each circle, we define a circle that is centered at this point that extends out for the given radius. We then use these as locations into the IDs array and set it to a unique ID for that particular circle. The result of IDs will be that which resembles the output of bwlabel. As such, if you want to extract the locations of where the idx circle is, you would do:
cir = IDs == idx;
For demonstration purposes, this is what the IDs array looks like once we scale the IDs such that it fits within a [0-255] range for visibility:
imshow(IDs, []);
Therefore, each shaded circle of a different shade of gray denotes a unique circle that was detected with imfindcircles.
However, the shades of gray are probably a bit ambiguous for certain coins as this blends into the background. Another way that we could visualize this is to apply a different colour map to the IDs array. We can try using the cool colour map, with the total number of colours to be the number of unique circles + 1 for the background. Therefore, we can do something like this:
cmap = cool(numel(radii) + 1);
RGB = ind2rgb(IDs, cmap);
imshow(RGB);
The above code will create a colour map such that each circle gets mapped to a unique colour in the cool colour map. The next line applies a mapping where each ID gets associated with a colour with ind2rgb and we finally show the image.
This is what we get:
Edit: the following solution is more adequate to scenarios where one does not require fitting the exact circumferences, although simple heuristics could be used to approximate the radii of the coins in the original image based on the centers found in the eroded one.
Assuming you have access to the Image Processing toolbox, try imerode on your original black and white image. It will apply an erosion morphological operator to your image. In fact, the Matlab webpage with the documentation of that function has an example strikingly similar to your problem/image and they use a disk structure.
Run the following code (based on the example linked above) assuming the image you submitted is called ima.jpg and is local to the code:
ima=imread('ima.jpg');
se = strel('disk',50);
eroded = imerode(ima,se);
imshow(eroded)
and you will see the image that follows as output. After you do this, you can use bwlabel to label the connected components and compute whatever properties you may want, for example, count the number of coins or detect their centers.

Matlab: Put an image ON TOP of a plot, after the data has been plotted

I have searched the internet for a solution to the question above but have had no luck up to now. I have been producing a number of 2D plots where the origin of (0,0 point) is represented by an image. I have made these by plotting the data on an image, where the image is all white apart from the desired symbol at the center point (so I can reshape it as needed). I then move the axis so they cross at the center point. This all works fine, but I now have to create a number of plots using ‘fill’ to place two shaded areas on the plot that overlap in the center. This causes the symbol to be difficult to see even using ‘alpha’.
I therefore have two options to get the desired effect, both requiring me to put an image on top of the figure after the data is plotted. These options are:
1) I place the image on top of the plot and apply alpha to it (May not look very good as it will mute the plot).
2) The better option would be to crop the image around the symbol and then position it on top of the plot so the image center is at the origin (I have no idea how to position the image this way).
Both methods need the image to be placed on top of the plot after the data is plotted. I have tried 'hold on' and calling 'figure(imagesc(Image))' neither work. I have Matlab 2012b but no toolboxes (so cannot use subimage etc.)
Thanks for any help you can give
You can set the transparency of individual pixels of an image using the 'AlphaData' option. So, you can plot the overlay as follows:
% plot your data normally
...
hold on
% assuming the overlay is a 11x11 image
image(-5:5,-5:5,image_matrix,'AlphaData',alpha_matrix);
image_matrix would obviously be the matrix with your image data stored in it, while alpha_matrix would be a matrix of the same size as image_matrix. Every pixel you want to show would have a value of 1, every pixel you want to hide (the white pixels) would be 0.

Drawing 256 squares/rectangles in MATLAB

I am trying to draw 256 small sized squares using MATLAB rectangle function. If I am drawing abut 10 squares then the following works fine:
for i=1:2:40
rectangle('Position',[5,3+i,0.3,0.3],...
'Curvature',[0,0],...
'LineStyle','-', 'faceColor', 'black')
end
axis off;
daspect([1,1,1])
But when I change the last value of for loop to 512 (to draw 256 squares), it is not printing properly:
Here is the magnified version of a section of the above image:
This image clearly shows some thing is wrong somewhere as the sides of the squares are not perfectly equal and that the squares are becoming smaller in size for higher no. of squares: Can anybody help me draw the squares perfectly with size not diminishing, ? (I don't have any issues with memory, and I can tolerate multiple pages scrolling down for covering entire squares )
It is a classical Moire effect. You can't show that much rectangles on your monitor, because there aren't enough pixels. Matlab does some down-sampling for you. Thus, you get another frequency, that did not exist originally.
I tried your code and it works fine even when the loop does 512 iterations - when you zoom-in in the final matlab figure. The artifacts you describe are probably caused by the monitor resolution or a low resolution while exporting to non-vector files.
Try to export your image as a vector file (eps or svg) to see that everything looks fine when you zoom in.

what does MajorAxisLength property in regionprop matlab function mean?

I am using regionprop function in matlab to get MajorAxisLength of an image. I think logically this number should not be greater than sqrt(a^2+b^2) in wich a abd b are the width and heigth of the image. but for my image it is. My black and white image contains a black circle in the center of the image. I think this is strange. Can anybody help me?
Thanks.
If you look at the code of regionprops (subfunction ComputeEllipseParams), you see that they use the second moment to estimate the ellipsoid radius. This works very well for ellipsoid-shaped features, but not very well for features with holes. The second moment increases if you remove pixels from around the centroid (which is, btw, why they make I-beams). Thus, the bigger the 'hole' in the middle of your image, the bigger the apparent ellipsoid radius.
In your case, you may be better off using the extrema property of regionprops, and to calculate the largest radius from there.