如何使用OpenCV对图像中的像素组进行分组和突出显示?

在对图像进行错误级别分析的过程中,我想使用OpenCV(仅显示单个图像而不显示差异)来突出显示像素变化。我知道输出图像的像素级值,但不确定将它们分组在一起并为其指定形状的方法(下面的示例中,用形状指定像素变化)。我想知道是否可以检测到像素较浅的圆圈并将其分组并为像素添加分组形状

输入图像:

如何使用OpenCV对图像中的像素组进行分组和突出显示?

结果图片:

如何使用OpenCV对图像中的像素组进行分组和突出显示?

LVCHA166 回答:如何使用OpenCV对图像中的像素组进行分组和突出显示?

如果我理解正确,您想在新图像中突出显示输入图像和输出图像之间的差异。为此,您可以采用定量方法,使用Image Quality Assessment: From Error Visibility to Structural Similarity中引入的结构相似性指数(SSIM)来确定图像之间的确切差异。该方法已经在scikit-image库中实现了图像处理。您可以将scikit-imagepip install scikit-image一起安装。

skimage.measure.compare_ssim()函数返回一个score和一个diff图像。 score代表两个输入图像之间的结构相似性指标,并且可以落在[-1,1]范围内,其值更接近一个代表更高相似性的值。但是,由于您只对两个图像的不同之处感兴趣,因此我们将重点关注diff图像。具体地,diff图像包含实际图像差异,其中较暗的区域具有更大的视差。较大的差异区域以黑色突出显示,而较小的差异以灰色突出显示。这是diff图片

enter image description here

如果您仔细观察,可能会由于.jpg有损压缩而出现灰色噪点区域。因此,为了获得更清晰的结果,我们执行形态学运算以平滑图像。如果图像使用无损图像压缩格式(例如.png),我们将获得更清晰的结果。整理完图像后,我们突出显示绿色的差异

enter image description here

from skimage.measure import compare_ssim
import numpy as np
import cv2

# Load images and convert to grayscale
image1 = cv2.imread('1.jpg')
image2 = cv2.imread('2.jpg')
image1_gray = cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY)
image2_gray = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY)

# Compute SSIM between two images
(score,diff) = compare_ssim(image1_gray,image2_gray,full=True)

# The diff image contains the actual image differences between the two images
# and is represented as a floating point data type in the range [0,1] 
# so we must convert the array to 8-bit unsigned integers in the range
# [0,255] before we can use it with OpenCV
diff = 255 - (diff * 255).astype("uint8")

cv2.imwrite('original_diff.png',diff)

# Perform morphological operations
kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
opening = cv2.morphologyEx(diff,cv2.MORPH_OPEN,kernel,iterations=1)
close = cv2.morphologyEx(opening,cv2.MORPH_CLOSE,iterations=1)
diff = cv2.merge([close,close,close])

# Color difference pixels
diff[np.where((diff > [10,10,50]).all(axis=2))] = [36,255,12]

cv2.imwrite('diff.png',diff)
,

我认为最好的方法是简单地对图像进行阈值处理并应用形态学转换。

我得到了以下结果。

阈值+形态:

Threashold + Morphological

选择最大的组件:

result

使用此代码:

cv::Mat result;
cv::Mat img = cv::imread("fOTmh.jpg");

//-- gray & smooth image
cv::cvtColor(img,result,cv::COLOR_BGR2GRAY);
cv::blur(result,cv::Size(5,5));

//-- threashold with max value of the image and smooth again!
double min,max;
cv::minMaxLoc(result,&min,&max);
cv::threshold(result,0.3*max,cv::THRESH_BINARY);
cv::medianBlur(result,7);

//-- apply Morphological Transformations
cv::Mat se = getStructuringElement(cv::MORPH_ELLIPSE,cv::Size(11,11));
cv::morphologyEx(result,cv::MORPH_DILATE,se);
cv::morphologyEx(result,cv::MORPH_CLOSE,se);

//-- find the largest component
vector<vector<cv::Point> > contours;
vector<cv::Vec4i> hierarchy;
cv::findContours(result,contours,hierarchy,cv::RETR_LIST,cv::CHAIN_APPROX_NONE);
vector<cv::Point> *l = nullptr;
for(auto &&c: contours){
    if (l==nullptr || l->size()< c.size())
        l = &c;
}

//-- expand and plot Rect around the largest component
cv::Rect r = boundingRect(*l);
r.x -=10;
r.y -=10;
r.width +=20;
r.height +=20;
cv::rectangle(img,r,cv::Scalar::all(255),3);


//-- result
cv::resize(img,img,cv::Size(),0.25,0.25);
cv::imshow("result",img);

Python代码:

import cv2 as cv

img = cv.imread("ELA_Final.jpg")

result = cv.cvtColor(img,cv.COLOR_BGR2GRAY);
result = cv.blur(result,(5,5));

minVal,maxVal,minLoc,maxLoc = cv.minMaxLoc(result)
ret,result = cv.threshold(result,0.3*maxVal,cv.THRESH_BINARY)
median = cv.medianBlur(result,7)

se = cv.getStructuringElement(cv.MORPH_ELLIPSE,(11,11));
result = cv.morphologyEx(result,cv.MORPH_DILATE,se);
result = cv.morphologyEx(result,cv.MORPH_CLOSE,se);

_,hierarchy = cv.findContours(result,cv.RETR_LIST,cv.CHAIN_APPROX_NONE)

x = []

for eachCOntor in contours:
    x.append(len(eachCOntor))
m = max(x)
p = [i for i,j in enumerate(x) if j == m]

color = (255,0) 
x,y,w,h = cv.boundingRect(contours[p[0]])
x -=10
y -=10
w +=20
h +=20
cv.rectangle(img,(x,y),(x+w,y+h),color,3)

img =  cv.resize( img,( 1500,700),interpolation = cv.INTER_AREA)
cv.imshow("result",img)
cv.waitKey(0)
本文链接:https://www.f2er.com/3139275.html

大家都在问