当我单独在Y中执行直方图匹配时,为什么Cr和Cb的直方图略有变化?

2024-04-20 16:11:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我用来匹配直方图和所有导入语句的代码:

import skimage.viewer
from skimage import data,io,color
from skimage import exposure
from skimage.exposure import match_histograms

ref1 = io.imread("drive/My Drive/Images_for_Adarsh/DSC_6139.JPG")
orig1 = io.imread("drive/My Drive/Images_for_Adarsh/DSC_6138.JPG")
ref = color.rgb2ycbcr(ref1)
orig = color.rgb2ycbcr(orig1)
f1 = match_histograms(orig, ref, multichannel=True)
f1[:,:,1] = orig[:,:,1]
f1[:,:,2] = orig[:,:,2]
f2 = color.ycbcr2rgb(f1)

我用来绘制直方图的代码:

import numpy as np
import cv2
from matplotlib import pyplot as plt
from matplotlib.pyplot import figure

img1 = cv2.imread("drive/My Drive/Images_for_Adarsh/DSC_6176.JPG")
img2 = cv2.imread("drive/My Drive/Y Histogram_Match RESULTS/Y6176.JPG")
img1f = cv2.cvtColor(img1,cv2.COLOR_BGR2YCR_CB)
img2f = cv2.cvtColor(img2,cv2.COLOR_BGR2YCR_CB)
histr1 = cv2.calcHist([img1f],[0],mask,[256],[0,255])
histr2 = cv2.calcHist([img2f],[0],mask,[256],[0,255])
f = figure(num=None, figsize=(9, 5), dpi=400, facecolor='w', edgecolor='k')
plt.subplot(1 ,2 , 1)
plt.plot(histr1,color = 'pink')
plt.xlim([0,275])
plt.xlabel("Luminance")
plt.ylabel("No. of Pixels")

plt.subplot(1 ,2 , 2)
plt.plot(histr2,color = 'pink')
plt.xlim([0,275])
plt.xlabel("Luminance")
plt.ylabel("No. of Pixels")

a1 = f.add_subplot(121)
a2 = f.add_subplot(122)
f.suptitle("DSC_6176 -- LUMINANCE HISTOGRAM", fontsize=16)
a1.title.set_text('ORIGINAL')
a2.title.set_text('RESULT(after Y value Histogram Matching)')


plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0.45, hspace=None)
plt.savefig("drive/My Drive/histograms Y Matching/Luminance6176.JPG",dpi = 400)

This is the Luminance histogram for the original image on the left and that of final result on the right

This is the Cb histogram for the original image on the left and that of final result on the right

This is the Cr histogram for the original image on the left and that of final result on the right

This is the input image

This is the reference image(I want the luminance histogram of the input image to this image)

这里,我对亮度(YCRCB中的“Y”值)执行直方图匹配。为此,我在所有3个通道(Y、Cr和Cb)上执行直方图匹配,然后从原始图像复制Cr和Cb,并将其放入最终图像中。因此,实际上,我的代码只修改亮度,而Cr和Cb不变。然而,当我为我的3个通道绘制直方图时,Cr和Cb通道有轻微变化。我无法理解这一点。为什么会这样?请帮忙!提前谢谢! 我已经添加了原始图像和结果的亮度(Y)、Cb和Cr直方图,供您参考


Tags: ofthefromimageimportnoneforon