Python Opencv - 无法更改图片的像素值

Sun*_*ndh 6 python opencv pixel

需要将下面给出的图片的白色像素更改为黑色,将黑色像素更改为白色在此处输入图片说明

    import cv2

    img=cv2.imread("cvlogo.png")
Run Code Online (Sandbox Code Playgroud)

带有白色背景的基本 opencv 徽标并将图片调整为固定的已知大小

    img=cv2.resize(img, (300,300))#(width,height)


    row,col=0,0
    i=0
Run Code Online (Sandbox Code Playgroud)

现在使用 for 循环检查每个像素的行和列位置

如果像素为白色,则将其更改为黑色;如果像素为黑色,则将其更改为白色。

    for row in range(0,300,1):
        print(row)
        for col in range(0,300,1):
            print(col)
            if img[row,col] is [255,255,255] : #I have used == instead of 'is'..but there is no change 
                img[row,col]=[0,0,0]
            elif img[row,col] is [0,0,0]:
                img[row,col]=[255,255,255]
Run Code Online (Sandbox Code Playgroud)

执行中没有错误,但不会将像素值分别更改为黑色或白色。更多 if 语句也没有执行..太混乱了..

    cv2.imshow('img',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

ajl*_*j25 5

我认为这应该有效。:) (我使用 numpy 只是为了获取宽度和高度值 - 你不需要这个)

import cv2

img=cv2.imread("cvlogo.png")
img=cv2.resize(img, (300,300))
height, width, channels = img.shape

white = [255,255,255]
black = [0,0,0]

for x in range(0,width):
    for y in range(0,height):
        channels_xy = img[y,x]
        if all(channels_xy == white):    
            img[y,x] = black

        elif all(channels_xy == black):
            img[y,x] = white

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)


Ant*_*gos 5

我不是很有经验,但我会使用 numpy.where(),它比循环更快。

import cv2
import numpy as np
import matplotlib.pyplot as plt

# Read the image
original_image=cv2.imread("cvlogo.png")
# Not necessary. Make a copy to plot later
img=np.copy(original_image)

#Isolate the areas where the color is black(every channel=0) and white (every channel=255)
black=np.where((img[:,:,0]==0) & (img[:,:,1]==0) & (img[:,:,2]==0))
white=np.where((img[:,:,0]==255) & (img[:,:,1]==255) & (img[:,:,2]==255))

#Turn black pixels to white and vice versa
img[black]=(255,255,255)
img[white]=(0,0,0)

# Plot the images
fig=plt.figure()
ax1 = fig.add_subplot(1,2,1)
ax1.imshow(original_image)
ax1.set_title('Original Image')
ax2 = fig.add_subplot(1,2,2)
ax2.imshow(img)
ax2.set_title('Modified Image')
plt.show()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


Sun*_*ndh 3

这也是解决这个问题的一种方法。学分:ajlaj25

    import cv2


    img=cv2.imread("cvlogo.png")
    img=cv2.resize(img, (300,300))
    height, width, channels = img.shape

    print(height,width,channels)

    for x in range(0,width):
        for y in range(0,height):
            if img[x,y,0] == 255 and img[x,y,1] == 255 and img[x,y,2] == 255:            
                img[x,y,0] = 0
                img[x,y,1] = 0
                img[x,y,2] = 0

            elif img[x,y,0] == 0 and img[x,y,1] == 0 and img[x,y,2] == 0:
                img[x,y,0] = 255
                img[x,y,1] = 255
                img[x,y,2] = 255
Run Code Online (Sandbox Code Playgroud)

img[x,y]表示x,y 坐标处的通道值 - 所有三个:[ch1,ch2,ch3] 。img[x,y,0] 是 ch1 通道在 x,y 坐标处的值。**

x 和 y 表示像素位置,而不是像素的 RGB 值。因此,img[x,y,0] 是 ch1 通道在 x,y 坐标处的值

**

    cv2.imshow('Coverted Image',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)