Reputation: 21
Using python (openCV2, tkinter etc) I've created an app (a very amateur one) to change blue pixels to white. The images are high quality jpgs or PNGS.
The process: Search every pixel of an image and if the 'b' value of BGR is higher than x, set pixel to white (255, 255, 255).
The problem: There are about 150 pictures to process at a time, so the above process takes quite long. It's around 9 - 15 seconds per iteration depending on the images size (resizing the image speeds up the process, but not ideal).
Here is the code (with GUI and exception handling elements removed for simplicity):
for filename in listdir(sourcefolder):
# Read image and set variables
frame = imread(sourcefolder+"/"+filename)
rows = frame.shape[0]
cols = frame.shape[1]
# Search pixels. If blue, set to white.
for i in range(0,rows):
for j in range(0,cols):
if frame.item(i,j,0) > 155:
frame.itemset((i,j,0),255)
frame.itemset((i,j,1),255)
frame.itemset((i,j,2),255)
imwrite(sourcecopy+"/"+filename, frame)
#release image from memory
del frame
Any help on increasing efficiency / speed would be greatly appreciated!
Upvotes: 1
Views: 4066
Reputation: 207465
Start with this image:
Then use this:
import cv2
im = cv2.imread('a.png')
# Make all pixels where Blue > 150 into white
im[im[...,0]>150] = [255,255,255]
# Save result
cv2.imwrite('result.png', im)
Upvotes: 5
Reputation: 736
Use cv2.threshold to create a mask using x threshold value.
Set the color like this : img_bgr[mask == 255] = [255, 0, 0]
Upvotes: 2