The following code is not rendering:
import cv2
import numpy as np
from time import sleep
hz = 30
bitmap = np.zeros((512,512,3),np.uint8)
for i in range(512):
sleep(1/hz)
bitmap[i,i,:] = 128
cv2.imshow("Color Image", bitmap)
cv2.waitKey(0)
cv2.destroyAllWindows()
What am I missing?
The waitKey should be inside the loop. The input to the waitKey is the number of milliseconds the frame should be rendered. When its 0, the frame is rendered indefinitely. Try this.
import cv2
import numpy as np
from time import sleep
hz = 30
bitmap = np.zeros((512,512,3),np.uint8)
for i in range(512):
sleep(1/hz)
bitmap[i,i,:] = 128
cv2.imshow("Color Image", bitmap)
cv2.waitKey(3)
cv2.destroyAllWindows()
Related
I am trying to create a program which will save images from pixel data obtained through openCV canny edge detection. Right now, the program saves a small image file in the correct path but the image file does not contain any of the data from the webcam.
An example of what should be saved in the image file:
picture of edge detected room
Versus what is actually saved: just a black rectangle
CODE BELOW:
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import numpy as np
from numpy import asarray
import PIL
from PIL import Image
import cv2
def LiveCamEdgeDetection_canny(image_color):
threshold_1 = 100 #LINES
threshold_2 = 50 #NOISE
image_gray = cv2.cvtColor(image_color, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(image_gray, threshold_1, threshold_2)
return canny
# Main calling function to initialize webcam and apply edge detection
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('Live Edge Detection', LiveCamEdgeDetection_canny(frame))
#cv2.imshow('Webcam Video', frame)
#print(LiveCamEdgeDetection_canny(frame))
# Store pixel data
pixels = [LiveCamEdgeDetection_canny(frame)]
image_todraw = np.array(pixels)
image_todraw = np.reshape(image_todraw, (720, 1280))
image_todraw *= 255
image_tosave = Image.fromarray(image_todraw.astype(np.uint8))
image_tosave.save('/Users/user/Desktop/destinationFolder/RETRY.jpeg', 'JPEG')
#print(image_tosave)
if cv2.waitKey(1) == 'p': #13 Enter Key
break
cap.release() # camera release
cv2.destroyAllWindows()
I appreciate all the help you can give me!
Remove the image_todraw *= 255 line.
Below is the output:
I don't want to convert it pixel wise, because of the complexity. Is there a library function to convert all white pixels to black at once, without affecting the other pixels?
import numpy as np
import cv2
img1 = cv2.imread('background.jpg')
Based on OpenCV forum: Replace a range of colors with a specific color in python
img[np.where((img==[255,255,255]).all(axis=2))] = [0,0,0]
Working example:
import cv2
import numpy as np
import time
cv2.namedWindow('window')
img = cv2.imread('image.jpg')
start = time.time()
img[np.where((img==[255,255,255]).all(axis=2))] = [0,0,0]
end = time.time()
print('time:', end-start)
cv2.imshow('window', img)
cv2.waitKey()
cv2.destroyAllWindows()
For image 512x341 time is 0.011182308197021484 (seconds)
EDIT: previous example with numpy is much faster then for-loops
import cv2
import numpy as np
import time
cv2.namedWindow('window')
img = cv2.imread('Obrazy/images/image.jpg')
y, x, z = img.shape # `y` is first in `shape`
print(x, y)
start = time.time()
for row in range(y):
for col in range(x):
if all(img[row,col] == [255,255,255]):
img[row,col] = [0,0,0]
end = time.time()
print('time:', end-start)
cv2.imshow('window', img)
cv2.waitKey()
cv2.destroyAllWindows()
For image 512x341 time is 1.5046415328979492 (seconds)
With
if (img[row,col] == [255,255,255]).all():
time is 2.443787097930908 (seconds)
Able to show the image through matplotlib, however unable to do it through cv2.imshow. The shape of the image is not consistent with opencv required formats. Require help on changing on changing it so it can be shown by the command cv2.imshow
test.jpg is a random jpg file from web
import numpy as np
import cv2
import matplotlib.pyplot as plt
import ReadIM
img = cv2.imread('test.jpg')
vbuff, vatts = ReadIM.extra.get_Buffer_andAttributeList('test.im7')
v_array, vbuff = ReadIM.extra.buffer_as_array(vbuff)
print (np.shape(v_array))
print (v_array[0])
print (np.shape(img))
# Showing image through matplotlib
plt.imshow(v_array[0])
plt.show()
#Showing image through cv2
cv2.imshow('image',v_array[0])
cv2.waitKey(0)
cv2.destroyAllWindows()
# Remove memory
#del(vbuff)
ReadIM.DestroyBuffer(vbuff)
ReadIM.DestroyAttributeListSafe(vatts)
test.im7
Normalizing the image to (0,255) will do the trick
img = cv2.normalize(img, None, 255,0,cv2.NORM_MINMAX,dtype = cv2.CV_8UC1)
cv2.imshow('image',img)
I created a white colored image matrix using numpy and now i want to blink any color over the right half of the image matrix that i created i am using the following code but it's not working as expected
import numpy as np
import cv2
import time
i=0
img=np.zeros((400,800,3),np.uint8)
img.fill(255)
while(i<=1):
img[0:400,400:800]=(153,0,255)
cv2.imshow('package',img)
time.sleep(5)
img[0:400,400:800]=(255,255,255)
cv2.imshow('package',img)
time.sleep(5)
img[0:400,400:800]=(153,0,255)
cv2.imshow('package',img)
i=i+1
cv2.waitKey(0)
cv2.destroyAllWindows()
`
You need "waitkey" just after imshow to ensure image redraws. waitkey(5000) means 5 seconds wait. "time.sleep(5)" is not necessary here:
import numpy as np
import cv2
import time
i=0
img=np.zeros((400,800,3),np.uint8)
img.fill(255)
while(i<=1):
img[0:400,400:800]=(153,0,255)
cv2.imshow('package',img)
cv2.waitKey(5000)
img[0:400,400:800]=(255,255,255)
cv2.imshow('package',img)
cv2.waitKey(5000)
img[0:400,400:800]=(153,0,255)
cv2.imshow('package',img)
cv2.waitKey(5000)
i=i+1
cv2.destroyAllWindows()
import numpy as np
import numpy as numpy
import cv2
windowsize_r = 8
windowsize_c = 8
img = cv2.imread('image test.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
for r in range(0,gray.shape[0] - windowsize_r, windowsize_r):
for c in range(0,gray.shape[0] - windowsize_c, windowsize_c):
window = gray[r:r+windowsize_r,c:c+windowsize_c]
hist = numpy.histogram(window,bins=256)
k = cv2.waitKey(0)
if k == 27:
cv2.destroyAllWindows()
I am trying to divide am image into 8x8 blocks. My image size is 320x240. So at the end there should be 1200 blocks.my code is not showing any error but nothing is observed as output. Can anyone suggest a better solution
you need to use cv2.imshow to actually show the patch before calling waitKey. eg:
cv2.imshow('wind',window)
cv2.waitKey(0)