Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I mage Processing USEING PYTHON: Reducing Spatial Resolution error: For every 33 block of the image (without overlapping), replace all corresponding 9 pixels by their
Image Processing USEING PYTHON: Reducing Spatial Resolution error:
For every 33 block of the image (without overlapping), replace all corresponding 9 pixels by their average.
Repeat this for 55 blocks and 77 blocks.
Please use fix the PYTHON CODE!!!
import cv2 import numpy as np IMAGE = cv2.imread(('C:/Users/faisa/Desktop/Urban Images/urban19.jpg')) IMAGE_GRAY = cv2.imread(('C:/Users/faisa/Desktop/Urban Images/urban19.gray.jpg')) def reduce_intensity(img, factor): # Quantizes an image by the specified factor""" return(img / factor) * factor #Displays an image cv2.imshow('Original', IMAGE) cv2.waitKey(0) cv2.destroyAllWindows() def shrink_resolution(img, factor): #Reduces resolution of an image by reducing and re enlarging it using the average of the neighbouring pixels""" shrunk = cv2.resize(img, (0,1), None, 1.0/factor, 1.0/factor, cv2.INTER_AREA) return cv2.resize(shrunk, (1,1), None, factor, factor, cv2.INTER_AREA) def main_shrink_resolution(): #Replaces pixel boxes by their average by shrinking and re enlarging the image""" img = cv2.imread(IMAGE_GRAY) images = [(n, shrink_resolution(img, n)) for n in (3,5,7,20,100)] show_images(images) def main_image_rotate(): #Rotates the image""" img = cv2.imread(IMAGE_GRAY) rows, cols, _ = img.shape rotation = cv2.getRotationMatrix2D((cols/2,rows/2),45,1) rotated = cv2.warpAffine(img, rotation, (cols, rows)) show_image('45', rotated) def main_image_blur(): #Applies a blur with different kernel sizes""" img = cv2.imread(IMAGE_GRAY) images = [(n, cv2.blur(img, (n,n))) for n in [3,10,20,100]] show_images(images) def main_image_boxfilter(): #Applies a box filter with different kernel sizes""" img = cv2.imread(IMAGE_GRAY) images = [(n, cv2.boxFilter(img, -1, (n,n))) for n in [3,10,20,100]] show_images(images) def main_image_filter2d(): #Applies a 2D filter with several nxn normalised kernels, ie nxn matrices of 1s""" img = cv2.imread(IMAGE_GRAY) images = [(n, cv2.filter2D(img, -1, np.ones((n,n),np.float32)/(n*n))) for n in [3,10,20,100]] show_images(images) def main_image_intensities(): #Displays intensities from original to just 2 colors""" img = cv2.imread(IMAGE_GRAY) images = [(2**exp, reduce_intensity(img, 2**exp)) for exp in range(0,8)] show_images(images) def main_image_to_grayscale(): img = cv2.imread(IMAGE) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.imwrite(IMAGE_GRAY, gray) cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if __name__ == '__main__': main_shrink_resolution()
ERROR
#Converts image to grayscale and saves""" OpenCV Error: Assertion failed (ssize.width > 0 && ssize.height > 0) in cv::resize, file C:\projects\opencv-python\opencv\modules\imgproc\src\imgwarp.cpp, line 3492 Traceback (most recent call last): , line 105, inmain_shrink_resolution() , line 33, in main_shrink_resolution images = [(n, shrink_resolution(img, n)) for n in (3,5,7,1,1)] , line 33, in images = [(n, shrink_resolution(img, n)) for n in (3,5,7,1,1)] , line 23, in shrink_resolution shrunk = cv2.resize(img, (0,1), None, 1.0/factor, 1.0/factor, cv2.INTER_AREA) cv2.error: C:\projects\opencv-python\opencv\modules\imgproc\src\imgwarp.cpp:3492: error: (-215) ssize.width > 0 && ssize.height > 0 in function cv::resize
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started