0

This is the code that I currently have. I want to avoid writing an image then loading it again and then copying it. Why isn't my code in the second part working?

import cv2

load_imaged = cv2.imread("image.png", 0)

# Apply GaussianBlur to reduce image noise if it is required
otsu_threshold, otsu_result = cv2.threshold(
    load_imaged, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU, )
# Optimal threshold value is determined automatically.

# Visualize the image after the Otsu's method application
cv2.imwrite("otsu.png", otsu_result)

hole_image = cv2.imread("otsu.png")

# copy image
img = hole_image.copy()
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow("Image", imgray)
cv2.waitKey()
cv2.destroyAllWindows()

and I'm trying to reference the image like this (in line 9) but it's returning an error.

Invalid number of channels in input image: 'VScn::contains(scn)' where 'scn' is 1

import cv2

load_imaged = cv2.imread("image.png", 0)

# Apply GaussianBlur to reduce image noise if it is required
otsu_threshold, otsu_result = cv2.threshold(
    load_imaged, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU, )
# copy image
img = otsu_result.copy()
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow("Image", imgray)
cv2.waitKey()
cv2.destroyAllWindows()

Any help is appreciated

1 Answer 1

2

You are trying to convert an gray-scale image into gray image.

Remove imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) line and you will get the result.

Sign up to request clarification or add additional context in comments.

2 Comments

That was a simple fix! Of course. I didn't think it would have been a problem. Thank you for the quick reply.
You're welcome. Make sure to check the shape of the image. (if gray-scale shape is width, height, if rgb width, height, 3). Also check this answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.