Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Opencv - Cannot change pixel value of a picture

Need to change the white pixels to black and black pixels to white of the picture given belowenter image description here

    import cv2

    img=cv2.imread("cvlogo.png")

A basic opencv logo with white background and resized the picture to a fixed known size

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


    row,col=0,0
    i=0

Now checking each pixel by its row and column positions with for loop

If pixel is white, then change it to black or if pixel is black,change it to white.

    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]

There is no error in execution but it is not changing the pixel values to black or white respectively. More over if statement is also not executing..Too much of confusion..

    cv2.imshow('img',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
like image 855
Sundara Moorthy Anandh Avatar asked Aug 10 '17 12:08

Sundara Moorthy Anandh


People also ask

How do I change the resolution of an image in OpenCV Python?

The first step is to create an object of the DNN superresolution class. This is followed by the reading and setting of the model, and finally, the image is upscaled. We have provided the Python and C++ codes below. You can replace the value of the model_path variable with the path of the model that you want to use.

How do I get the pixel value of an image in OpenCV?

Figure 5: In OpenCV, pixels are accessed by their (x, y)-coordinates. The origin, (0, 0), is located at the top-left of the image. OpenCV images are zero-indexed, where the x-values go left-to-right (column number) and y-values go top-to-bottom (row number). Here, we have the letter “I” on a piece of graph paper.

How do I move pixels in OpenCV?

You can simply use affine transformation translation matrix (which is for shifting points basically). cv::warpAffine() with proper transformation matrix will do the trick. where: tx is shift in the image x axis, ty is shift in the image y axis, Every single pixel in the image will be shifted like that.


Video Answer


2 Answers

I am not very experienced, but I would do it using numpy.where(), which is faster than the loops.

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()

enter image description here

like image 148
Antoniou Giorgos Avatar answered Sep 20 '22 02:09

Antoniou Giorgos


I think this should work. :) (I used numpy just to get width and height values - you dont need this)

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()
like image 40
ajlaj25 Avatar answered Sep 22 '22 02:09

ajlaj25