user3313031
user3313031

Reputation: 11

Sobel filter is working on the original i/p image but not on the copy of it

I want to apply Sobel and other filter at an image but i can see the results of filter only if i use the filters at original input image. But if i implement the filter at a copy of inout image then nothing happens and just a white output image appears.

In the following code i am implementing the filter at original inputImage but i want to get results by implementing it at copyImage.

inputImage=imread('tex.png');
copyImage=double(inputImage);

for i=1:size(C,1)-2
    for j=1:size(C,2)-2
        %Sobel mask for x-direction:
        Gx=((2*C(i+2,j+1)+C(i+2,j)+C(i+2,j+2))-(2*C(i,j+1)+C(i,j)+C(i,j+2)));
        %Sobel mask for y-direction:
        Gy=((2*C(i+1,j+2)+C(i,j+2)+C(i+2,j+2))-(2*C(i+1,j)+C(i,j)+C(i+2,j)));

        %The gradient of the image
        inputImage(i,j)=sqrt(Gx.^2+Gy.^2);
    end
end

Upvotes: 1

Views: 96

Answers (1)

Notlikethat
Notlikethat

Reputation: 20934

imshow with a double argument expects the values to be in the range 0.0 (black) to 1.0 (white). Anything greater than 1 is clipped to white. Since presumably inputImage is integer, using double(inputImage) doesn't rescale and you end up with double values in ranges like 0-255 or 0-65535 depending on the bit depth, which therefore just show as white.

If you need double format, use im2double which rescales the values correctly. Otherwise, just create an integer copy with copyImage = inputImage.

If you don't have the Image Processing Toolbox for im2double, you can do an unsigned integer-to-double image conversion with:

dblimg = double(img) ./ double(intmax(class(img)));

Upvotes: 4

Related Questions