![](/img/trans.png)
[英]Toggling of floodfill colors (either black or white depending on the pixel color selected)
[英]How to change the set of pixel colors in contatc with black color
考虑这张图片:
我试图在python
中使用此代码:
import numpy as np
from PIL import Image
im = Image.open('image.png')
data = np.array(im)
r1, g1, b1 = 255, 255, 255 # Original value
r2, g2, b2 = 0, 0, 255 # Value that we want to replace it with
red, green, blue = data[:,:,0], data[:,:,1], data[:,:,2]
mask = (red == r1) & (green == g1) & (blue == b1)
data[:,:,:3][mask] = [r2, g2, b2]
im = Image.fromarray(data)
但是我把所有的白色像素都换成了红色。 但也可能是UNIX
方法建议。
请发布输入图像的无损版本。 有损图像会修改像素值,从而产生影响处理的伪像。 我重新创建了您的图像并将其保存为无损 PNF 文件。
我正在使用 OpenCV 来获得您想要的结果。 我用原始输入的非零元素创建了一个掩码。 然后,我使用Flood-fill用你想要的颜色填充外部形状。 如果你AND
两个图像,可以获得最终图像。
让我们看看代码:
# import opencv:
import cv2
# image path
path = "D://opencvImages//"
fileName = "rectsLossless.png"
# Reading an image in default mode:
inputImage = cv2.imread(path + fileName)
# Grayscale image:
grayscaleImage = cv2.cvtColor(inputImage, cv2.COLOR_BGR2GRAY)
# Get non-zero mask:
binaryThresh = 1
_, binaryMask = cv2.threshold(grayscaleImage, binaryThresh, 255, cv2.THRESH_BINARY)
该位创建非零像素掩码:
这将有助于将所有非白色元素归零。 该图像是掩码的第一部分。 现在,让我们用红色填充外部形状。 这是通过三个步骤实现的:
# Get image dimensions:
(imageHeight, imageWidth) = inputImage.shape[:2]
# Get image center:
xCenter = int(0.5 * imageWidth)
yCenter = int(0.5 * imageHeight)
# Get flood-fill target color
floodColor = inputImage[yCenter, xCenter]
print("Flood Color: %s" % floodColor)
# numpy array to tuple
floodColor = (int(floodColor[0]), int(floodColor[1]), int(floodColor[2]))
第一步获取实际的填充颜色。 我想红色或多或少位于图像的中心。 然后,第二步涉及用白色填充所有“前景”像素。 让我们在左上角播种:
# Flood fill at top left corner:
leftCorner = (1, 1)
whiteColor = (255, 255, 255)
cv2.floodFill(inputImage, None, leftCorner, whiteColor)
这是结果:
请注意,部分位于红色矩形之外的形状现在如何全部由白色连接。 让我们再次填充,但这次使用我之前提取的红色:
# Second Flood-fill
cv2.floodFill(inputImage, None, leftCorner, floodColor)
这会产生以下图像:
让我们通过将结果与原始非零掩码进行AND
运算来创建最终图像:
# Create final image:
outImage = cv2.bitwise_and(inputImage, inputImage, mask=binaryMask)
这是最终结果:
这个问题非常接近这个问题。
我的解决方案也很接近......
假设 colors 是黑色白色和红色(一般情况可能比较棘手),我们可以使用以下阶段:
cv2.floodFill
)。cv2.floodFill
)。代码示例:
import cv2
import numpy as np
img = cv2.imread('red_white_black.jpg')
# Copy the original image to img2
img2 = img.copy()
# Fill the black background with white color
cv2.floodFill(img2, None, seedPoint=(0, 0), newVal=(255, 255, 255), loDiff=(50, 50, 50), upDiff=(50, 50, 50))
cv2.imshow('black background', img2) # Show img2 for testing
# Fill the white background with black color
cv2.floodFill(img2, None, seedPoint=(0, 0), newVal=(0, 0, 0), loDiff=(50, 50, 50), upDiff=(50, 50, 50))
cv2.imshow('white background', img2) # Show img2 for testing
# Copy the red color channel from the original image to img2
img2[:, :, 2] = img[:, :, 2]
cv2.imshow('img2', img2) # Show img2 for testing
cv2.waitKey()
cv2.destroyAllWindows()
红色周围的黑色边缘是因为原始图像是 JPEG 而不是 PNG(颜色不纯),并且红色不是纯红色。
我们可以使用以下代码修复它(代码不是很优雅)...
red = img[:, :, 2]
r = np.median(img[:, :, 2][red > 50])
g = np.median(img[:, :, 1][red > 50])
b = np.median(img[:, :, 0][red > 50])
mask = np.logical_and(img[:, :, 0] > 100, img2[:, :, 0] <= 100)
img3 = img2.copy()
img3[:, :, 2][mask] = r
img3[:, :, 1][mask] = g
img3[:, :, 0][mask] = b
img3[:, :, 2] = cv2.morphologyEx(img3[:, :, 2], cv2.MORPH_CLOSE, np.ones((3, 3), np.uint8))
img3[:, :, 1] = cv2.morphologyEx(img3[:, :, 1], cv2.MORPH_OPEN, np.ones((3, 3), np.uint8))
img3[:, :, 0] = cv2.morphologyEx(img3[:, :, 0], cv2.MORPH_OPEN, np.ones((3, 3), np.uint8))
cv2.imshow('img3', img3)
cv2.waitKey()
cv2.destroyAllWindows()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.