matlab image blurring using fspecial - matlab

I am trying to write a matlab program for image blurring. I am required to use fspecial('average') and conv2 function. So far I have written the following code:
x=imread('ghoul.jpg');
subplot(211),imshow(x)
h=fspecial('average');
y=conv2(double(x),double(h));
subplot(212),imshow(y)
size of x is 250X250 uint8
The problem with the code is that it displays the original image fine but the image is only blurred at the bottom and white in the remaining area.
So far I have guessed that I haven't specified the size in h. But I am having problem in how to define the size in h. Whether it should be the size of x or not. It would be helpful if someone can just tell me how to write the size or another tip.
Thanks for your help.

The problem with the matlab code is that it was using imshow on double data type which caused the image intensity value to distort (barely visible or invisible in certain areas of the image). The filtered image needed rescaling of intensity values and as #eigenchris pointed out using:
imshow(y,[])
readjusted the image intensity values and the image was blurred perfectly.
side note: The size of the filter didn't had any effect on image distortion. (size is just used as a measure to how much you want to blur the image)

Related

Camera Calibration Toolbox for Matlab Undistort Image loses image borders

I am using the Camera Calbration Toolbox from Cal Tech to undistort images. However, when I do so the images lose their borders in the final undistorted image. I am curious as to whether there is a way to avoid this, as the entire image is important. Thanks in advance.
Do you have access to Matlab's Computer Vision System Toolbox? It includes a function undistortImage that allows you to set the output view to include the entire undistorted image, like so:
outImg = undistortImage(inImg, cameraParams, 'OutputView', 'full');
As far as I know, the Camera Calibration Toolbox's undistort function doesn't include this functionality. If you don't have the above toolbox, you could try zero-padding your image with enough border that the actual image will remain in the undistorted frame, and then you could crop the result using the actual image's bounding box. This should probably be a last resort though. Undistorting the padded image won't yield exactly the same results as with the original image. Pad as little as possible!

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.

transformed image should always visible

I am trying to transform an image using Bi-linear interpolation, my input image is I, I have my affine matrix [A], which will give me transformed image I', according to bi-linear interpolation I am taking inverse of affine matrix inv([A]) and applying that to every point of output image(which is all zero at initial level), as we cant guarantee that output image size can be of any size, so first I found the bounds so I can get the size of the output image,
Now I have input image, Affine matrix, and output image which have atleast that size in which transformed image can be saved easily, But If I apply backward backward method of warping, according to that I have to iterate through every pixel of output image(which is zero right now), I want my transformed image at the center so my transformed image should always be visible, any idea how can I do that ?
Note I don't want to use matlab's built in function.
EDIT
If I transformed my A Image I got B, but You see corner of the image got cropped, I want those to be shown as well.
When rotating a rectangle from the upright position to a diagonal one, the vertical distance between the highest and lowest point will increase.
Now there are two approaches you can take:
Put the new picture in a bigger environment
OR
Rescale the rotated picture to make it fit in the original sized environment.

How Do I Find The Bounding Box For All Regions?

I'm using the MNIST digit images for a machine learning experiment, and I'm trying to center each image based on position, rather than the center of mass that they are centered on by default.
I'm using the regionprops class, BoundingBox method to extract the images. I create a B&W copy of the greyscale, use this to determine the BoundingBox properties (regionprops works only B&W images) and then apply that on the greyscale original to extract the precise image rectangle. This works fine on ~98% of the images.
The problem I have is that the other ~2% of images has some kind of noise or errant pixel in the upper left corner, and I end up extracting only that pixel, with the rest of the image discarded.
How can I incorporate all elements of the image into a single rectangle?
EDIT: Further research has made me realise that I can summarise and rephrase this question as "How do I find the bounding box for all regions?". I've tried adjusting a label matrix so that all regions are the same label, to no avail.
You can use an erosion mask with the same size of that noise to make it totally disappear " using imerode followed by imdilate to inverse erosion ", or you can use median filter

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.