【问题标题】:comparing images pixel by pixel逐像素比较图像
【发布时间】:2017-08-03 11:13:27
【问题描述】:

我正在处理一些图像。我得到了一个abc.tif 图像(彩色图像)。我是这样读的:

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

我对其执行一些操作并将其转换为仅包含两个值 0 和 255 的二进制图像(使用阈值),它们存储在 img 图像中,其中 img 创建如下:

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

我使用imwrite("myimage.jpg",img);保存这张图片

我想将myimage.jpg 图像与另一个二进制图像manual.gif 逐像素进行比较,以检查一个图像是否与另一个图像重复,但您可以注意到问题是 OpenCv 不支持 .gif 格式,所以我需要转换将其转换为 .jpg ,因此图像会发生变化,现在这两个图像都将得出结论,因为即使它们相同,也可能是不同的图像。现在要做什么?

实际上我正在研究视网膜血管分割,这些图像可以在 DRIVE 数据库中找到。

我收到了这些图片。原图:

我对其执行一些操作并从中提取血管,然后创建一个二进制图像并存储在一些 Mat 变量 img 中,如前所述。现在我得到了另一个无法加载的图像(.gif 图像),如下所示:

现在我想将我的 img 图像(二进制)与我无法加载的给定 .gif 图像(上图)进行比较。

【问题讨论】:

  • 只需使用 IrfanView 等外部工具将您的 GIF 转换为 PNG,OpenCV 就会读取它。此外,不要将结果保存为 JPG,因为这是一种会改变您的值的有损格式。也将其另存为无损 PNG。
  • 或者使用ImageMagick,命令就这么简单convert input.gif output.png
  • 感谢您的宝贵意见!
  • 很酷的问题!是为了认证吗?这些网络在分支数量方面有多相似?
  • 是的,这是一种身份验证。 80-85% 相似。

标签: matlab opencv


【解决方案1】:

使用 ImageMagic 以批处理模式将您的 .gif 转换为 .PNG。您也可以使用system("convert img.gif img.png") 调用即时转换它。

我不确定,像素比较是否会给您带来好的结果。同一图像的偏移偏移将导致匹配不良。 编辑作为一个想法。也许计算重心并移动/旋转两个图像以使其具有相同的原点可能会有所帮助。

考虑使用矩、弗里曼链或其他模态稳健形状比较方法。

【讨论】:

  • 感谢 Valentin...您的宝贵意见。
【解决方案2】:

首先,您将希望以彼此相同的格式使用图像@Adi 提到 jpg 在 cmets 中是有损的,这是正确的,因此在完成任何工作之前不应使用。 MATLAB - image conversion

您还希望图像具有相同的大小。您可以使用 size 函数比较它们,然后填充它们以添加像素以使尺寸相同。填充可以稍后删除,只需观察填充是如何添加的,以免影响您的操作。

您还需要研究旋转,考虑将图像放入频域并旋转图像以对齐频谱。

下面是一个简单的像素比较代码,像素比较不是特别准确的比较。即使是最轻微的未对齐也会导致误报或误报。

%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');

【讨论】:

    猜你喜欢
    • 2015-04-30
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多