通过坐标或根据坐标将二进制图像像素更改为黑色的图像掩蔽

Image masking by coordinate or changing binary image pixel to black based on the the coordinate

我有这个二进制图像,我想删除 (x, y) 坐标上方的对象(将所有白色像素转换为黑色)。这是初始二值图像image

这是绘制了 x 和 y 坐标点的二值图像。我想删除或更改坐标以上的所有白色像素区域,包括坐标 x&y 点,并将其更改为黑色,只获取坐标以下的区域。

仅供参考,我使用以下代码将 x 和 y 坐标的最小值以上的像素更改为黑色。但我想更改每个 x 和 y 坐标上方的所有区域,包括坐标值。

如果您对某些代码有任何建议,我将不胜感激?非常感谢。

# The x& y coordinates are as following: 
x_axis = [357.85987957681857, 1343.856973606684, 1193.444389180811, 1054.4435778213533, 920.8840743484891, 834.5163114772306, 765.2724445311123, 695.4422963676367, 626.3723193199884, 548.5159239129672, 473.3864264519827, 415.95327695853143, 2316.797956919471, 1343.856973606684, 1425.0931289410505, 1521.1495595490387, 1632.3052834725456, 1744.0579233904436, 1866.4648358093323, 1976.6461686748466, 2065.953479619877, 2139.260636596274, 2199.6220916860434, 2245.0254022731133, 2272.5521801650925, 2283.2074846563964] 


y_axis = [ 402.0816733067729, 303.6283524904215, 306.47956013914586, 306.2518624638496, 309.42755708674883, 310.55640319868576, 314.7508122651324, 321.7233756421446, 333.7052985557767, 346.05232533978085, 359.70697325309675, 378.72710281397315, 398.2068373269085, 303.3486590038314, 301.54526194124645, 299.79525494813197, 299.7740607761268, 300.14989694332405, 301.8288092006239, 311.1532369144714, 323.43818847918556, 338.60411297399423, 354.74463422095664, 368.8686654180594, 378.3843659129424, 382.4001483708864 ] 

image = cv2.imread("image.jpg")

plt.figure(figsize=(30, 20))
implot = plt.imshow(image )
plt.scatter(x_axis ,y_axis, c='red', s=5)
plt.show()

h, w = image .shape[:2]
# print (h,w)

for i in range(h):
    for j in range (w):
        if i < min(y_axis) and j > min (x_axis):
            image [i,j] = 0
fig, ax = plt.subplots(figsize=(30,20))
ax.imshow(image)


也许图像实际上是具有三个(红、绿、蓝)通道的彩色图像。如果是这样,您可以尝试 image [i,j] = [0,0,0] 而不是 image [i,j] = 0.

我是这样解决的,我把坐标(0,0)和(image.shape[1],0)加到现有的x-axis和y-axis坐标上.因此,使用 cv2.fillpoly 我可以在坐标上方绘制一个黑色的多边形,直到坐标 (0,0) 或图像的左上角和右上角的 (image.shape[1],0)的形象。实现代码如下:

x_axis = [357.85987957681857, 1343.856973606684, 1193.444389180811, 1054.4435778213533, 920.8840743484891, 834.5163114772306, 765.2724445311123, 695.4422963676367, 626.3723193199884, 548.5159239129672, 473.3864264519827, 415.95327695853143, 2316.797956919471, 1343.856973606684, 1425.0931289410505, 1521.1495595490387, 1632.3052834725456, 1744.0579233904436, 1866.4648358093323, 1976.6461686748466, 2065.953479619877, 2139.260636596274, 2199.6220916860434, 2245.0254022731133, 2272.5521801650925, 2283.2074846563964] 
y_axis = [ 402.0816733067729, 303.6283524904215, 306.47956013914586, 306.2518624638496, 309.42755708674883, 310.55640319868576, 314.7508122651324, 321.7233756421446, 333.7052985557767, 346.05232533978085, 359.70697325309675, 378.72710281397315, 398.2068373269085, 303.3486590038314, 301.54526194124645, 299.79525494813197, 299.7740607761268, 300.14989694332405, 301.8288092006239, 311.1532369144714, 323.43818847918556, 338.60411297399423, 354.74463422095664, 368.8686654180594, 378.3843659129424, 382.4001483708864 ] 

image = cv2.imread("image.jpg") # initially the image were binary image 

x_axis.extend([0,image.shape[1]])   #adding axis (0,0) & (image.shape[1],0)
y_axis.extend ([0, 0])
x_axis, y_axis= zip (*sorted(zip(x_axis, y_axis)))
points = list(zip(x_axis,y_axis))
points = np.array (points)
preprocessed_image = cv2.fillPoly(image, np.int32([points]), color=(0, 0, 0)) # the colors can be changed 

fig, ax = plt.subplots(figsize=(30,20))
ax.imshow(preprocessed_image)