I want to output an image with image segmentation, but I can't.

Traceback (most recent call last):
File "color.py3", line 39, in
cv.imshow ('marks', markers)
cv2.error: OpenCV (4.4.0-dev) /export/ruby/GIT/OpenCV/opencv/modules/highgui/src/precomp.hpp:137: error: (-215: Assertion failed) src_depth! = CV_16F&&src_depth ! = CV_32S in function'convertToShow'

Corresponding source code
import numpy as np
import os
import sys
vnum = str (sys.version_info [0]) + "." + str (sys.version_info [1])
UsrLocalLibPath ='/ usr/local/lib/python' + vnum +'/ dist-packages'
if not os.path.exists (UsrLocalLibPath + "/ cv2") == True:
    print ("user local lib path for cv2 does not exist:" + UsrLocalLibPath)
    exit (-1)
sys.path.append (os.path.join (os.path.dirname (__ file__), UsrLocalLibPath))
import cv2 as cv
from matplotlib import pyplot as plt
img = cv.imread ('coins.png')
gray = cv.cvtColor (img, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold (gray, 0,255, cv.THRESH_BINARY_INV + cv.THRESH_OTSU)
#noise removal
kernel = np.ones ((3,3), np.uint8)
opening = cv.morphologyEx (thresh, cv.MORPH_OPEN, kernel, iterations = 2)
# sure background area
sure_bg = cv.dilate (opening, kernel, iterations = 3)
#Finding sure foreground area
dist_transform = cv.distanceTransform (opening, cv.DIST_L2,5)
ret, sure_fg = cv.threshold (dist_transform, 0.7 * dist_transform.max (), 255,0)
# Finding unknown region
sure_fg = np.uint8 (sure_fg)
unknown = cv.subtract (sure_bg, sure_fg)
#Marker labelling
ret, markers = cv.connectedComponents (sure_fg)
# Add one to all labels so that sure background is not 0, but 1
markers = markers + 1
# Now, mark the region of unknown with zero
markers [unknown == 255] = 0
markers = cv.watershed (img, markers)
img [markers == -1] = [255,0,0]
cv.imshow ('marks', markers)
cv.waitKey (0)
cv.destroyAllWindows ()
What I tried

The program that displays the image with imshow does not work, but I can't figure out what is the cause by looking at the error. I am glad if you could tell me.

Supplementary information (FW/tool version, etc.)

Please provide more detailed information here.

  • Answer # 1

    -215: Assertion failedIs an error that occurs when you pre-pay the error "This is not true" before judging the data, such as the type does not match or the data does not exist in the first place.

    src_depth! = CV_16F&&src_depth! = CV_32S
    Feelings of OpenCV: I wanted imshow to be neither int32 nor float16.

    It means that.

    # cv.imshow ('marks', markers)
    cv.imshow ('marks', markers.astype (np.float32))

    Will it not work if I replace it with?
    Here, I was able to capture something that looked like a contour by moving.