Answered step by step
Verified Expert Solution
Question
1 Approved Answer
CENG203 DATA STRUCTURES 2023-2024 Fall, Take Home Midterm Submission deadline: 23.12.202311:59 pm - You should write all your code in Python language. - Cheating is
CENG203 DATA STRUCTURES 2023-2024 Fall, Take Home Midterm Submission deadline: 23.12.202311:59 pm - You should write all your code in Python language. - Cheating is highly discouraged. - Olearn system can have a limited upload capacity. If you could not upload your results, you can share them with me via Dropbox, or send me private YouTube video links for each part's results. 1) Dancing with myself ( 50 pts.) In this homework, we will use an image sequence from the famous dancing cat video to get familiar with OpenCV, NumPy and MoviePy libraries. Some frames which will be used are given in Figure 1. Figure 1: Some example greenscreen cat frames The first part of the homework focuses on an introductory problem especially for students who are not comfortable with numpy and OpenCV libraries. 1 In this part, we will - Read a background image and some cat images from the specified folders. - Place the cat images onto the left of background image. - Flip the cat images and place them onto right of background image. -Write the obtained frames as a video. To read an image in OpenCV, you can use cv2.imread function. As shown in the code below, you can use cv2. imshow to show the image inside a window and to give a close condition to the window, you can use cv2.waitKey (0) which unpauses the program by a keyboard click. First thing we should do here is to resize the background image according to our cat frames. As our cat frames have a shape of (360,640,3), we should resize height of the background image according to this. background height = background.shape [0] background_width = background.shape[1] ratio =360/ background_height background = cv2.resize (background, (int(background_width * ratio), 360)) print (background.shape) Then, we should read every image inside the cat folder and place it onto the background image. An example placement is given below. image = cv2.imread ( main_dir + '/cat5.png' ) foreground = np.logical_or(image [:,:,1]150) \#The pixels having cat image. nonzero_x, nonzero_y = np.nonzero(foreground) \#The 'foreground' variable here is only a True-False map with the same size. \#Using np.nonzero function we can find the locations of True values. nonzero_cat_values = image[nonzero_x, nonzero_y, : ] \# A matrix of shape ( . . . 3) containing the pixel values be longing to the cat part. new_frame = background.copy ( ) new_frame [nonzero_x, nonzero_y, :] = nonzero_cat_values \#To the previously obtained indices, the cat part is placed . new_frame = new frame [:,:,[2,1,0]] \#The frame here is currently in RGB order. However, the moviepy library defaultly uses BGR order. Thus, it may be good to reverse the channels. After obtaining the frames like that, to use moviepy.editor.ImageSequenceClip, we should append the frames to a list. Then, using set_audio function, we can place a background music and using write_videofile we can save this composition as a video. images_list =[] \# Some code here images_list.append(new_frame) clip = mpy.ImageSequenceClip(imageslist, fps=25 ) audio = mpy.AudioFileclip('selfcontrol part.wav').set_duration(clip.duration) clip = clip.set_audio(audioclip=audio) 2 clip.write_videofile('part1_video.mp4', codec='libx264') If everything goes well, you will obtain a video. A frame from the video is given in Figure 2. Figure 2: I wo cats on the dancetloor 2) Dancing with my friend ( 30 pts.) In this part of the homework you will do histogram matching between video frames and a target image. To do this, - Obtain a histogram for cat parts of each frame and use the average of them. This will give us average cat histogram. Obtain histogram for each channel. - Make histogram matching for the new cat using a target image. You can select the bin count according to the results. You are also free to select the target image. An example is shown in Figure 3. Figure 3: Histogram matching result
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