有没有一种方法可以使用OpenCV将图像转换为灰度?

huangapple go评论57阅读模式
英文:

Is there a way to convert img to grayscale with opencv

问题

我正在尝试使用Python和cv2将图像转换为灰度我看到了一些其他答案它们在使用matlab而我不打算使用有没有办法我可以解决这个问题图像正常启动一切都好只是无法转换以下是代码

```python
import cv2

# 选择要检测人脸的图像
img = cv2.imread('RDJ.png')

# 必须转换为灰度
grayscaled_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


#
cv2.imshow('Face Detector', img)
cv2.waitKey()

我已经尝试了各种方法来解决它,但我无法找到任何解决方案。

英文:

I am trying to convert an image into grayscale using python and cv2. I saw a few other answers and they were using matlab, which I am not going to use. Is there anyway that I can fix this issue. The image boots up fine and everything it just wont convert. Here is the code.

import cv2

# Choose an image to detect faces in
img = cv2.imread('RDJ.png')

# Must convert to grayscale
grayscaled_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


#
cv2.imshow('Face Detector', img)
cv2.waitKey()

I have tried to fix it using different things but I cant figure out any solutions.

答案1

得分: 2

你需要更改你的代码的倒数第二行:

cv2.imshow('人脸检测器', 灰度图像)

因为显示的图像是原始图像。

英文:

You need to change the penultimate line of your code:

cv2.imshow('Face Detector', grayscaled_img)

Because the image showed is the original.

huangapple
  • 本文由 发表于 2023年1月9日 07:07:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定