下面,我有一个函数可以拍摄两个图像imageOne
和imageTwo
。这些图像将逐像素进行扫描,以确定它们的“百分比匹配”,我希望知道以下功能是否对该目的有效。它是手工编写的,因此可能存在一些问题,但总的来说,我认为它是可靠的。感谢您提供的所有信息。
还添加了getColorSimilarity()
函数。
double MainWindow::imageMatchPercent(QImage imageOne, QImage imageTwo)
{
QImage compOne = imageOne;
QImage compTwo = imageTwo;
if (imageOne.isNull())
{
QMessageBox::warning(this, "Image Loading Error", "Image One is empty.");
return (double)999.999;
}
if (imageTwo.isNull())
{
QMessageBox::warning(this, "Image Loading Error: ", "Image Two is empty.");
return (double)999.999;
}
if (imageOne.size() != imageTwo.size())
{
QSize imageOneSize = imageOne.size();
QSize imageTwoSize = imageTwo.size();
averageSize = getAverageSize(imageOneSize, imageTwoSize);
}
else
averageSize = imageOne.size();
int correctValues = 0;
int currentValue = 0;
int totalPixels = averageSize.width() * averageSize.height();
for (int x = 0; x < averageSize.width(); x++)
{
for (int y = 0; y < averageSize.height(); y++)
{
QColor pixelOneColor = compOne.pixel(x, y);
QColor pixelTwoColor = compTwo.pixel(x, y);
double colorDifference = getColorSimilarity(pixelOneColor, pixelTwoColor);
if (pixelOneColor.alpha() == 255 && pixelTwoColor.alpha() == 255)
{
if (colorDifference >= 75)
correctValues += 1;
currentValue += 1;
}
}
}
double percent = ((double)correctValues / (double)totalPixels) * (double)100.00;
return percent;
}
double MainWindow::getColorSimilarity(QColor colorOne, QColor colorTwo)
{
int redOne = colorOne.red();
int greenOne = colorOne.green();
int blueOne = colorOne.blue();
int redTwo = colorTwo.red();
int greenTwo = colorTwo.green();
int blueTwo = colorTwo.blue();
int redDif = abs(redOne - redTwo);
int greenDif = abs(greenOne - greenTwo);
int blueDif = abs(blueOne - blueTwo);
double percentRedDiff = (double)redDif / 255;
double percentGreenDiff = (double)greenDif / 255;
double percentBlueDiff = (double)blueDif / 255;
return 100 - (((percentRedDiff + percentGreenDiff + percentBlueDiff) / 3) * 100);
}
答案 0 :(得分:3)
请注意,您现在使用的averageSize对象用于遍历图像,即现在设置的方式,如果两个图像的大小不同,则averageSize.height或.width会大于其中之一。一张图像的尺寸,如果将图像存储为数组,则可能会出现分割错误,我对QImage不太熟悉,所以可以,但是如果将图像存储为数组,则可能会出错限制了您的编写方式。
答案 1 :(得分:1)
这似乎是比较两个图像的最有效方法。我不确定Qt是否支持多线程,但是如果您想使处理更快,可以将图像划分为四个象限,然后让每个线程依次处理每个像素。