I want to blink an image using a python code - python

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

Related

Render a continually changing numpy array to screen at 30Hz with opencv

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

How to convert all white(255,255,255) pixels to black (0,0,0) using a library function?

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)

im7 image files to openCV

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)

Python creating video from images using opencv

I was trying to create a video to show the dynamic variation of the data, like just continuously showing the images one by one quickly, so I used images (the images just called 1,2,3,4,.....) and wrote the following code:
import cv2
import numpy as np
img=[]
for i in range(0,5):
img.append(cv2.imread(str(i)+'.png'))
height,width,layers=img[1].shape
video=cv2.VideoWriter('video.avi',-1,1,(width,height))
for j in range(0,5):
video.write(img)
cv2.destroyAllWindows()
video.release()
and a error was raised:
TypeError: image is not a numpy array, neither a scalar
I think I used the list in a wrong way but I'm not sure. So where did I do wrong?
You are writing the whole array of frames. Try to save frame by frame instead:
...
for j in range(0,5):
video.write(img[j])
...
reference
You can read the frames and write them to video in a loop. Following is your code with a small modification to remove one for loop.
import cv2
import numpy as np
# choose codec according to format needed
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter('video.avi', fourcc, 1, (width, height))
for j in range(0,5):
img = cv2.imread(str(i) + '.png')
video.write(img)
cv2.destroyAllWindows()
video.release()
Alternatively, you can use skvideo library to create video form sequence of images.
import numpy as np
import skvideo.io
out_video = np.empty([5, height, width, 3], dtype = np.uint8)
out_video = out_video.astype(np.uint8)
for i in range(5):
img = cv2.imread(str(i) + '.png')
out_video[i] = img
# Writes the the output image sequences in a video file
skvideo.io.vwrite("video.mp4", out_video)
You can use this pip package. It provides CLI commands to make video from images.
img_to_vid.py -f images_directory

Divide image into 8x8 block : Python

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)

Categories