def get_motion_vectors(self, image2, searcher, MAD_threshold = None): """ 1) Divide self.image into blocks of 8x8 pixels 2) for each block: 4) get the X and Y 5) search block in image2 from X and Y, moving from 0 to P pixel right, left, top, bottom 6) block found? 7) if yes, get the new X and Y 8) if no, return 0 """ if isinstance(self.image, QImage): image1 = ImageConverter.qtimage_to_pil_image(self.image) else: image1 = self.image images1_pixels = image1.load() if isinstance(image2, QImage): image2 = ImageConverter.qtimage_to_pil_image(image2) images2_pixels = image2.load() width = image1.size[0] height = image1.size[1] vectors = [] for block_x_num in range(0, width/searcher.block_size): block_x_pos = searcher.block_size*block_x_num for block_y_num in range(0, height/searcher.block_size): block_y_pos = searcher.block_size*block_y_num (new_x, new_y, MAD, MAD_checks_count) = searcher.search(images1_pixels, block_x_pos, block_y_pos, images2_pixels) valid_vector = True if MAD_threshold and MAD > MAD_threshold: #Discard the vector if the MAD is over ranged valid_vector = False if valid_vector: #if (block_x_pos != new_x) or (block_y_pos != new_y): vector = { 'x': block_x_pos, 'y': block_y_pos, 'to_x' : new_x, 'to_y': new_y, 'MAD': MAD, 'MAD_checks_count': MAD_checks_count} vectors.append(vector) return vectors
def _draw_compressed_frame2(self): if len(self.vectors) > 0: self._draw_motion_vectors() zero_vectors_blocks_count = 0 new_blocks_count = 0 moved_vectors_blocks_count = 0 self._draw_frame(self.image_2, self.ui.frame2CompressedGraphicsView) scene = self.ui.frame2CompressedGraphicsView.scene() image2_new = QImage(self.image_2) sameBlockPen = QPen(Qt.black, 1, Qt.SolidLine) movedBlockPen = QPen(Qt.green, 1, Qt.SolidLine) for v in self.vectors: x = int(v["x"]) y = int(v["y"]) to_x = int(v["to_x"]) to_y = int(v["to_y"]) MAD = v["MAD"] if x == to_x and y == to_y: #The block is the same of the previous frame. Transmit a zero vector zero_vectors_blocks_count += 1 scene.addRect(x, y, self.get_block_size(), self.get_block_size(), sameBlockPen, QBrush(Qt.SolidPattern)) else: if MAD < self.ui.MADThresholdSpingBox.value(): #The block is moved moved_vectors_blocks_count += 1 scene.addRect(x, y, self.get_block_size(), self.get_block_size(), movedBlockPen, QBrush(Qt.green, Qt.SolidPattern)) moved_block_image = self.image_1.copy(x,y, self.get_block_size(), self.get_block_size()) ImageConverter.draw_image_into_image(moved_block_image, image2_new, to_x, to_y) else: #A new block of the frame is needed new_blocks_count += 1 #Draw the reconstructed Frame scene = QGraphicsScene() scene.addPixmap(QPixmap.fromImage(image2_new)) self.ui.frame2ReconstructedGraphicsView.setScene(scene) #Show the statistics zero_vectors_blocks_percent = (zero_vectors_blocks_count*100/len(self.vectors)) new_blocks_percent = (new_blocks_count*100/len(self.vectors)) moved_vectors_blocks_percent = (moved_vectors_blocks_count*100/len(self.vectors)) compression_ratio = 100 - new_blocks_percent - moved_vectors_blocks_percent/3 self.ui.zeroVectorsPercentLabel.setText("%d %%" %zero_vectors_blocks_percent ) self.ui.newBlocksPercentLabel.setText("%d %%" % new_blocks_percent) self.ui.movedVectorsPercentLabel.setText("%d %%" % moved_vectors_blocks_percent) self.ui.compressionRatioLabel.setText("%d %%" %compression_ratio) total_mads_checked = 0 for v in self.vectors: total_mads_checked += v["MAD_checks_count"] self.ui.MADsCheckedLabel.setText("%d" %total_mads_checked) #Calculate the PSNR PSNR = ImageComparator.calculate_PSNR(ImageConverter.qtimage_to_pil_image(self.image_2), ImageConverter.qtimage_to_pil_image(image2_new), self.image_2.width(), self.image_2.height()) self.ui.psnrFrame2Label.setText("%d" % PSNR)