繁体   English   中英

如何在Qt中比较两个RGB图像?

[英]How to compare two RGB images in Qt?

我需要比较两个彩色RBG图像,并获得逐像素差异的结果图像。 有什么想法我怎么能在qt中做到这一点?

我将不胜感激任何帮助或建议。

这是基于此QtForum问题的替代方法:

void substract(const QImage &left, const QImage &rigth, QImage &result)
{
  int w=min(left.width(), rigth.width());
  int h=min(left.height(),rigth.height();
  w=min(w, result.width());
  h=min(h, result.height();
  //<-This ensures that you work only at the intersection of images areas

  for(int i=0;i<h;i++){
    QRgb *rgbLeft=(QRgb*)left.constScanLine(i);
    QRgb *rgbRigth=(QRgb*)rigth.constScanLine(i);
    QRgb *rgbResult=(QRgb*)result.constScanLine(i);
    for(int j=0;j<w;j++){
        rgbResult[j] = rgbLeft[j]-rgbRigth[j];
    }
  }
}

首先,RGB图像是包含3个通道(R,G,B)的3维矩阵

要获得差异,您可以简单地减去矩阵。

如果您使用的是OpenCv,请考虑以下代码,否则,您可以遍历矩阵并分别减去每个位置。

#include <cv.h>
#include <highgui.h>

using namespace cv;

Mat img = imread("...");
Mat img2 = imread("...");

Mat diff_img = img - img2;

使用QImage可以在像素级别上进行迭代,只需将RBG差异输出到第三张图像中即可。

QRgb QImage::pixel(int x, int y) const
void QImage::setPixelColor(int x, int y, const QColor &color)

请记住,顺序迭代行以获得最佳性能。 意味着该行应该是您的内部循环。 很多时候人们本能地做相反的事情,可能是因为大多数人将宽度优先于高度,因此将行作为外部循环。

bool ImagesAreSimilar(QImage * img1,QImage * img2){

if (img1->isNull()||img2->isNull())
{
  return false ;
}
if (img1->height()!=img2->height()) {
    return false ;
}
if (img1->width()!=img2->width()) {
    return false ;
}
 auto pixel1 = img1->bits();
 auto pixel2 = img2->bits();
 bool similar=true;
for (int y = 0; y < img1->height(); y++)
{
    for (int x = 0; x < img1->width(); x++)
    {

        if (    (pixel1[0]!=pixel2[0])||
                (pixel1[1]!=pixel2[1])||
                (pixel1[2]!=pixel2[2])||
                (pixel1[3]!=pixel2[3])) {
                return false ;
        }
        pixel1  += 4;
        pixel2 += 4;
    }
}

返回相似

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM