Need to change the white pixels to black and black pixels to white of the picture given below
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()
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.
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.
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.
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()
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With