Opencv Python realizes the paint filling function in PS, one click filling color and the possible reasons for opencv’s frequent errors

First of all, first solve the opencv error problem, once up on the error report headache
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-z4706ql7\opencv\modules\highgui\src\window.cpp:1274: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function ‘cvShowImage’

This version is probably not downloaded well, delete and start again

 

pip uninstall opencv-python

Then download the command

pip3 install opencv-contrib-python

Nice successfully solved the problem. Of course, there may be errors in the path with Chinese or spaces, and sometimes errors will be reported.

No more code, no more nonsense

import cv2 as cv
import numpy as np

def fill_color_demo(image):    #Define the function to fill the color with one click
    Img2 = image.copy() # make a copy of the input image
    h, w = image.shape[:2] #Get the length and width of the image
    mask = np.zeros([h+2, w+2],np.uint8) #mask must be row and column plus 2, and must be uint8 single-channel array, fill the edges need more than 2 pixels, otherwise it will report an error
    cv.floodFill(Img2, mask, (100, 100), (127, 127, 127), (100, 100, 100), (50, 50 ,50), cv.FLOODFILL_FIXED_RANGE)
    #cv.floodFill, parameter 1,: indicates the input image, parameter 2: indicates the mask of a single channel, parameter 3: indicates the starting point of the flooding algorithm, parameter 4 indicates the color of the fill, and parameters 5,6 indicate the maximum positive and negative difference between the currently observed pixel point and the neighboring pixel points
    #x coordinates are from left to right, y coordinates are from top to bottom
    cv.imshow("result", Img2) # display the result image

img = cv.imread('. /1.jpg') # read in the image
cv.imshow('input', img) #Show the input image
fill_color_demo(img) #Transfer the input image into the defined fill color function
cv.waitKey(0)    

Then there is the result, as shown below

On the left is the original image of input and on the right is the output image. The color is (127127) gray and the coordinates are (100100). You can change the color coordinates as you like. It’s still very fun

Remember to like, pay attention to and collect


Read More: