I am converting an RGB image into YCbCr and then want to compute the laplacian pyramid for the same. After color conversion, I am experimenting with the code give on the Image Pyramid tutorial of OpenCV to find the Laplacian pyramid of an image and then reconstruct the original image. However, if I increase the number of levels in my code to a higher number, say 10, then the reconstructed image(after conversion back to RGB) does not look the same as the original image(image looks blurred - please see below link for the exact image). I am not sure why this is happening. Is it suppose to happen when the levels increase or is there anything wrong in the code?
frame = cv2.cvtColor(frame_RGB, cv2.COLOR_BGR2YCR_CB)
height = 10
Gauss = frame.copy()
gpA = [Gauss]
for i in xrange(height):
Gauss = cv2.pyrDown(Gauss)
gpA.append(Gauss)
lbImage = [gpA[height-1]]
for j in xrange(height-1,0,-1):
GE = cv2.pyrUp(gpA[j])
L = cv2.subtract(gpA[j-1],GE)
lbImage.append(L)
ls_ = lbImage[0]
for j in range(1,height,1):
ls_ = cv2.pyrUp(ls_)
ls_ = cv2.add(ls_,lbImage[j])
ls_ = cv2.cvtColor(ls_, cv2.COLOR_YCR_CB2BGR)
cv2.imshow("Pyramid reconstructed Image",ls_)
cv2.waitKey(0)
For reference, please see the reconstructed image and the original image.
Reconstructed Image
Original Image
Don't use np.add()
or np.substract()
. They perform a clipping. Use the direct - and + matrix operator. In other words, use:
L = gpA[j-1] - GE
Instead of:
L = cv2.subtract(gpA[j-1],GE)
pyrDown blurs an image and downsamples it, loosing some information. Saved pyramid levels (gpA[]
here) contain smaller and smaller image matrices, but don't keep rejected information details (high-frequency ones).
So reconstructed image cannot show all original details
From tutorial: Note: When we reduce the size of an image, we are actually losing information of the image.
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