- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直认为将图像从彩色转换为灰度很简单:每个像素的强度将是每个颜色 channel 强度的平均值。但是我注意到 cv2.COLOR_RGB2GRAY
和 cv2.COLOR_BGR2GRAY
给出了不同的结果。当我用它们进行试验时,我还发现它也不同于每个颜色 channel 的强度平均值。
附言当我发现时,我完全困惑了
img_read_as_color[:,:,0]/3+img_read_as_color[:,:,1]/3+img_read_as_color[:,:,2]/3 == (img_read_as_color[:,:,0]+img_read_as_color[:,:,1]+img_read_as_color[:,:,2])/3
但是当显示为图像时
(img_read_as_color[:,:,0]+img_read_as_color[:,:,1]+img_read_as_color[:,:,2])/3
看起来像
img_read_as_color[:,:,0]+img_read_as_color[:,:,1]+img_read_as_color[:,:,2]
有人可以向我解释为什么会这样吗?
import matplotlib.pyplot as plt
import cv2
sample = r'G:\Python\knight-mare\screenshots\2020-07-12-02-40-44.jpg'
img_read_as_grayscale = cv2.imread(sample, cv2.IMREAD_GRAYSCALE)
img_read_as_color = cv2.imread(sample, cv2.IMREAD_COLOR)
img_RGB_to_grayscale = cv2.cvtColor(img_read_as_color, cv2.COLOR_RGB2GRAY)
img_BGR_to_grayscale = cv2.cvtColor(img_read_as_color, cv2.COLOR_BGR2GRAY)
plt.imshow(img_read_as_grayscale)
plt.title('img_read_as_grayscale')
plt.show()
plt.imshow(img_read_as_color)
plt.title('img_read_as_color')
plt.show()
plt.imshow(img_RGB_to_grayscale)
plt.title('img_RGB_to_grayscale')
plt.show()
plt.imshow(img_BGR_to_grayscale)
plt.title('img_BGR_to_grayscale')
plt.show()
channel_avg_div_separately = img_read_as_color[:,:,0]/3+img_read_as_color[:,:,1]/3+img_read_as_color[:,:,2]/3
channel_avg_div_together = (img_read_as_color[:,:,0]+img_read_as_color[:,:,1]+img_read_as_color[:,:,2])/3
channel_sum = img_read_as_color[:,:,0]+img_read_as_color[:,:,1]+img_read_as_color[:,:,2]
plt.imshow(channel_avg_div_separately)
plt.title('channel_avg_div_separately')
plt.show()
plt.imshow(channel_avg_div_together)
plt.title('channel_avg_div_together')
plt.show()
plt.imshow(channel_sum)
plt.title('channel_sum')
plt.show()
最佳答案
gray_pixel = 0.114 * blue_pixel + 0.299 * red_pixel + 0.587 * green_pixel
documentation 中也提到了这一点。因此,预计 RGB2GRAY 和 BGR2GRAY 会给出不同的结果。
img_read_as_color[:,:,0]/3+img_read_as_color[:,:,1]/3+img_read_as_color[:,:,2]/3
和
(img_read_as_color[:,:,0]+img_read_as_color[:,:,1]+img_read_as_color[:,:,2])/3
回想一下
cv2.imread
返回一个 uint8 numpy 数组。因此,后一个操作(在除法之前将所有 channel 组合在一起)会导致溢出(实际上,在这种情况下,ipython3 给了我一个运行时警告)。在标记为
channel_avg_div_together
和
channel_sum
的图像中也可以看到类似溢出的伪影。
关于python-3.x - 为什么 cv2.COLOR_RGB2GRAY 和 cv2.COLOR_BGR2GRAY 会给出不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62855718/
我到处都找了很多,找不到我的问题的答案。我试图从这个线程复制一个文本检测软件(Extracting text OpenCV)但是在代码的末尾有一条消息错误说没有匹配的矩形,即使我已经在上面绘制了一个并
我已经彻底搜索过,但没有找到直接的答案。 将 opencv 矩阵 (cv::Mat) 作为参数传递给函数,我们传递的是智能指针。我们对函数内部的输入矩阵所做的任何更改也会改变函数范围之外的矩阵。 我读
在我的应用程序中,我有一个通过引用接收 cv::Mat 对象的函数。这是函数的声明: void getChains(cv::Mat &img,std::vector &chains,cv::
我正在使用 Qt 编写一个 GUI 程序,并使用 OpenCV 进行一些视频处理。我在主 GUI 线程的标签中显示 OpenCV 进程(在单独的线程中)的结果。 我遇到的问题是 cv::waitKey
Mat a = (Mat_(3,3) = 2 int dims; //! the number of rows and columns or (-1, -1) when the arr
我尝试运行下面的代码,但出现错误。我正在为名为“Mat::at”的 OpenCV 函数创建一个包装器,并尝试使用“G++”将其编译为 Ubuntu Trusty 上的“.so”。我在下面列出了“.cp
我在 C# 中使用 EmguCV,当我想从网络摄像头抓取帧时遇到问题,语句中出现红色下划线: imgOrg = capturecam.QueryFrame(); error: Cannot impli
我正在尝试从另外两个矩阵生成一个 cv::Mat C,以便获得第三个矩阵,该矩阵由通过组合矩阵 A 和 B 的一维点生成的二维点构成。 我的问题是,我尝试的所有操作都只是连接矩阵,并没有真正将每个点与
我用 cv.imread在 python 中读取 png 文件。然后当我使用 cv.imwrite立即保存图像的功能我然后发现图像中的颜色略有变化。我正在尝试在此图像上执行字符识别,而 OCR 在 p
我尝试将 cv::bitwise_not 转换为 double 值的 cv::Mat 矩阵。我申请了 cv::bitwise_not(img, imgtemp); img是0和1的CV_64F数据。但
我正在尝试使用函数 cv.glmnet 找到最佳的 lambda(使用 RIDGE 回归)以预测某些对象的归属类别。所以我使用的代码是: CVGLM<-cv.glmnet(x,y,nfolds=34,
我有这个方法: static void WriteMatVect(const std::string& filename, const std::vector& mats); ... void Fil
下面的转换是我想要做的。 对于源图像中的每个图 block ,我知道每个角的坐标,并且我知道输出图像中每个对应角的坐标,所以我可以调用 cvWarpPerspective 扭曲每个图 block ,然
我必须在C++ / CLI中的托管和非托管代码中都使用OpenCV。 我正在尝试在托管代码中使用Emgu CV来包装OpenCV对象,但是在进行转换时遇到了麻烦。 我该怎么做: Emgu::CV::M
我正在尝试在 cv::Mat 中使用 CV_32FC4,以便它存储 RGBA32 图像。但是当我使用 cv::imwrite 将其保存为 png 文件时,结果文件始终是一个空图像。 例如,我创建了这样
无法在 VS 2017 中设置 OpenCV。我做错了什么?是的,我已将所有其他帖子设为红色。 代码: #include "opencv2/highgui/highgui.hpp" u
我有两个(相同大小,相同类型)cv:Mat 让我们称它们为 A,B。我还有另一个 cv::Mat,它是一个掩码(0 和 1 值或其他值,0 和 255 也适用)让我们称它为 M。 我需要构造一个新的
使用 OpenCV 中实现的 Scalar 类,我不明白这段代码有什么区别: Mat test; test = Scalar::all(0); 还有这个: Mat test = Scalar::all
我对这行代码感到困惑: cv::Mat_::iterator 我知道 Mat_ 属于 cv 命名空间和 vec3b 也。但是之后的最后一个 :: 操作符和 iterator 让我感到困惑!它也属于 c
我想优雅地将 Mat 转换为 Vec3f。目前我是这样做的: Mat line; Vec3f ln; ln[0] = line.
我是一名优秀的程序员,十分优秀!