I would like to gray out Python & OpenCV, but an AttributeError appears.

Asked 2 years ago, Updated 2 years ago, 121 views

US>Tools used
·Python 3.6
·OpenCV 4.5 ( 最新I included the latest version)
·Anaconda 3.0

I touched on image processing for the first time.At first, I thought it would be really fun, but I got an error and the program didn't work at all.

I was told that it doesn't work because the OpenCV version is different, but to be honest, I don't know what the difference is.

Error Occurring

AttributeError: module 'cv2.cv2' has no attribute 'cvtcolor'

Code you want to move

import cv2
image=cv2.imread(r"C:\library_cv\sky_006.jpg)
gray=cv2.cvtcolor(image,cv2.COLOR.BGR2GRAY)
cv2.imwrite(r "C:\write_cv\gray_006.jpg")

The goal is to use cv2.countNonZero in the above code to produce a white percentage.
*The image name is virtual, but it is generally the same.

python python3 opencv

2022-09-30 18:06

1 Answers

There are several types including the error in the question.
It should work by doing the following:

import cv2
image=cv2.imread(r"C:\library_cv\sky_006.jpg")
gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imwrite(r "C:\write_cv\gray_006.jpg", gray)
  • imread filename " is not closed (it must be a transcription error when questioning)
  • cvtcolor in cvtcolor starts with a capital letter cvtColor
  • cv2.COLOR.BGR2GRAY is a mistake in cv2.COLOR_BGR2GRAY (connect with _ instead of .)
  • imwrite must specify an object in the image in the second parameter

Link comments correctly here
Calculate the area ratio between white and black from binary images with Python, OpenCV

The bw_image in the above article is an object of binary image data.
Here are the specifications:
cv::threshold
Image threshold processing
Python:cv2.threshold(src,thresh,maxval,type[,dst])→retval,dst

Click here for instructions on how to use it.
OpenCV – How does image processing binarize and how to use cv2.threshold()
cv2.threshold()

  • Return value
    • retval: Threshold (return value to know the threshold automatically determined using cv2.THRESH_OTSU, cv2.THRESH_TRIANGLE)
    • dst:binary image
  • retval: Threshold (return value to know the threshold automatically determined using cv2.THRESH_OTSU, cv2.THRESH_TRIANGLE)
  • dst:binary image


2022-09-30 18:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.