Image enhancement using edge extraction - matlab

We can extract the edge of an image in MATLAB using the function edge()
My question is how can I recombine the edge with the original image to get an image with enhanced edges to increase the sharpness of the image.

What you are looking for already exists!
The image filter in question is called an 'unsharp mask'. It basically uses the edge data of an image to sharpen it. To elucidate, what it actually does in a manner of sorts is to use the difference of the image and a blurred version of it and then use that to sharpen the image. You can read more about it here.
To use it, simply do something like the following:
>> my_image = imread('lena.jpg');
>> subplot(1,2,1);
>> imshow(my_image);
>> subplot(1,2,2);
>> imshow(imfilter(my_image,fspecial('unsharp')));
This would yeild:
As you can see, the second image is visibly sharper and this is done by "adding" the edge data to the original image through the use of the unsharp mask.

Forget edge(). Just call imsharpen()

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!

how to remove the edge of an image after edge extraction using MATLAB?

I have an image in gray scale and I would like to remove the edge that is extracted by using an edge extraction method in MATLAB.
Is it possible to do it?
Below is my code that perform the edge extraction :
%load the image
A=imread('MikuBW.jpg');
%predefined edge extraction method in MATLAB
g=edge(A,'canny');
%plot results
subplot(2,1,1);
imshow(A),title('ORIGINAL');
subplot(2,1,2);
imshow(g),title('CANNY');
!(http://i.imgur.com/uS2Xxwf.jpg) [the result of original image and after edge extraction][1]
how do I remove the edge that is extracted in "CANNY" from the original image?
thank you for any help possible! =)
if you just want the "Canny" part to be removed, i.e. to be replaced with zeros, you can simply write:
A(g) = 0;
however, a more "natural" approach will be to blur the image, so it will have less edges in the first place:
H = fspecial('disk',5);
A = imfilter(A,H,'replicate');

Unite endpoints of edge with line

I'm trying to make an object recognition program using a k-NN classifier. I've got a bunch of images for the training part of the classifier and a bunch of images to recognize. Those images are in grayscale and there's an object (only its edge) per image. I need to calculate their center of mass so I use
img=im2bw(img)
and then regionprops(img,'centroid').
The problem is that some of those edges aren't closed so regionprops doesn't work then. I tried eroding the image (the edge is black, white background) but the endlines of those edges are too apart from eachother. I tried using bwmorph function to do so but still can't make it work.
Any ideas?
EDIT
I'm adding some images in case anyone wants to try:
Use morphological operation
Use a closing operation to make your structures filled.
1. As first step prepare your image data
im = imread('your image.jpg');
% Get first channel as gray scale information
im = im(:,:,1);
% Threshold it for simplicyty, you may work on grayscale too.
im1 = logical(im > 128);
2. Use a simple block shaped structuring element
The structuring element is defined by:
strel=ones(3,3);
You may use disk shaped elements or whatever gives the best result to you.
3. Apply structuring element a couple of times
Apply the strel a couple of times with an erosion operator to your original image to close your figure:
for i=1:20
im1 = imerode(im1,strel);
end
4. Dilate the image to get back to original shape
Next step is to dilate the image to get back to your original outer shape:
for i=1:20
im1 = imdilate(im1,strel);
end
Final result
The final result should be suitable to get a sufficiently precise center or gravity.

Convert just the content of a figure to an image in MATLAB

I am currently using getframe() and frame2im in MATLAB to convert a figure of a plot to an image.
I just realized that this is working almost like a screenshot of the figure, with all the axes and labels taken into account as well.
How can I convert JUST the contents of the figure (aka the "plot") into an image?
I don't really want to save all of them to file first.
You can use the getframe / cdata idiom. What this will do is that if you call getframe on the current frame in focus without any parameters, it will return a structure to you that contains a structure element called cdata. This stores the RGB pixel array of just the figure contents themselves. The axes and labels are not captured - only what is painted onto the figure is captured.
Here's an example to get you started:
im = imread('cameraman.tif');
imshow(im);
h = getframe;
out = h.cdata;
figure;
imshow(out); %// Should give you the contents within the imshow frame.
FWIW, I also answered this same question here, though it was for a different situation: keep new image when drawing lines by dragging the mouse in matlab
As far as i know, cdata DOES NOT WORK. I had a major problem recently with the same thing - the only work around i could find is to crop each image after using getframe and cdata - this will work fine for images that are all the same size (ugly as it is - you just need to find the grey edges in the image), but if the images are all different, this wont work (well, it wont work well. there might be some way to automatically adjust the crop size)

MATLAB. Inverse crop images.

I would like to crop an image but I want to retain the part of image that is outside of the rectangle. How can this can be done?
It seems that with imcrop only the part within the rectangle can be retained.
An image in Matlab is represented by a matrix, just like any other matrix, you can read more about representation forms here.
It seems that what you want to do is to take the area that you don't want and change the values of the corresponding cells in the matrix to the color that you want to put instead (each cell in the matrix is a pixel in the image). That is if you know the place where your unwanted data is.
If you don't know where it is, and want to use the tool given by imcrop to manually choose the "cropped" area, you can take the resulting matrix, and find the part of the original image which is an exact match with the cropped part, and to color it as you wish.
The code for doing this:
I=imread('img_9.tif');
I2=imcrop(I,[60,50,85,85]);
n_big=size(I);
n_small=size(I2);
for j1=1:(n_big(1)-n_small(1))
for j2=1:(n_big(2)-n_small(2))
Itest=I(j1:j1+n_small(1)-1,j2:j2+n_small(2)-1,:);
if ( Itest == I2)
I(j1:j1+n_small(1)-1,j2:j2+n_small(2)-1,:) = zeros(n_small(1),n_small(2),3);
end
end
end
figure(1);
imshow(I);
figure(2);
imshow(I2);
The results of my test were:
original:
cropped:
resulting image:
maybe what you want to do is first a mask with the inverse area of what you want to crop and save this result.