Compare pixels by pixel

I am working on some images. I have been given an image abc.tif

(color image). I read it like this:

Mat test_image=imread("abc.tif",IMREAD_UNCHANGED);

      

I perform some operations on it and convert it to some binary image (using a threshold) containing only two values ​​0 and 255 which are stored in the img image where the img is generated like this:

Mat img(584,565,CV_8UC1);   %//(so now img contains only 0 and 255)

      

I am saving this image using imwrite("myimage.jpg",img);

I want to compare an image myimage.jpg

with another binary image manual.gif

pixel by pixel to check if one image is duplicate, but as you can see, the problem is that OpenCv does not support the .gif format, so I need to convert that to .jpg. and because of this, the image changes, and now both images will be completed, as different images can be even though they are the same. What to do now?

I am actually working on segmentation of retinal blood vessels and these images are in the DRIVE database.

These images have been given to me. Original Image:

enter image description here

I do some operations on it and extract the blood vessels from it and then create a binary image and store in some variable Mat img as discussed earlier. Now I have another image (.gif image) that I cannot load as shown below:

enter image description here

Now I want to compare my img image (binary) with a given .gif image (above) which I cannot load.

+3


source to share


2 answers


Use ImageMagic to convert your .gif to .PNG in batch. You can also convert it on the fly using a call system("convert img.gif img.png")

.

I'm not sure if comparing the pixels will give you a good result. Offset offset of the same image will result in a bad match. EDIT Like an idea. Perhaps calculating the centers of gravity and displacement / rotation of both images with the same source can help here.



Consider using moments, free chaining, or other forms of comparison.

+1


source


first you will want to use images in the same format as each other. @Adi mentioned by jpg is lost in the comments, which is correct, so shouldn't be used until maybe some work has been done. MATLAB - Image Conversion

you will also want the images to be the same size. you can compare them using the size function and then place them to add pixels to make the measurements the same. the pad can always be removed later, just watch the padding added so it doesn't affect your operations.

you will also need to look at rotations, consider placing the image in the frequency domain and rotating the image to flatten the spectrum.



below is a simple pixel comparison code, pixel comparison is not particularly accurate for comparison. even the slightest alignment of the gaps will cause false negatives or false positives.

%read image
test_image1 = imread('C:\Users\Public\Pictures\Sample Pictures\Desert.jpg');
test_image2 = imread('C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg');

%convert to gray scale
gray_img1 = rgb2gray(test_image1);
gray_img2 = rgb2gray(test_image2);

% threshold image to put all values greater than 125 to 255 and all values
% below 125 to 0
binary_image1 = gray_img1 > 125;
binary_image2 = gray_img2 > 125;

%binary image to size to allow pixel by pixel checking
[row, col] = size(binary_image1);

% initialize the counters for similar and different pixelse to zero
similar = 0;
different = 0;

%two loops to scan through all rows and columns of the image. 
for kk = 1 : row
    for yy = 1 : col
        %using if statement with isequal function to compare corresponding
        %pixel values and count them depending ont he logical output of 
        %isequal 
        if isequal(binary_image1(kk,yy), binary_image2(kk,yy))
           similar = similar + 1;
        else
            different = different + 1;
        end
    end
end

% calculate the percentage difference between the images and print it
total_pixels = row*col;
difference_percentage = (different / total_pixels) * 100;
fprintf('%f%% difference between the compared images \n%d pixels being different to %d total pixels\n', difference_percentage, different, total_pixels )

% simple supbtraction of the two images
diff_image = binary_image1 - binary_image2;


%generate figure to show the original gray and corresponding binary images
%as well as the subtraction
figure
subplot(2,3,1)
imshow(gray_img1);
title('gray img1');

subplot(2,3,2)
imshow(gray_img2);
title('gray img2');

subplot(2,3,4)
imshow(binary_image1);
title('binary image1');

subplot(2,3,5)
imshow(binary_image2);
title('binary image2');

subplot(2,3,[3,6])
imshow(diff_image);
title('diff image');

      

0


source







All Articles