import cv2 import numpy as np import gradio as gr def pirahansiah_threshold_method_find_threshold_values_2(grayImg): #http://www.jatit.org/volumes/Vol95No21/1Vol95No21.pdf #https://pdfs.semanticscholar.org/05b2/d39fce4e8a99897e95f8c75416f65a5a0acc.pdf assert grayImg is not None, "file could not be read, check with os.path.exists()" #img = cv2.GaussianBlur(self.grayImg, (3, 3), 0) img = grayImg # Initialize an array to store the PSNR values for each threshold value psnr_values = np.zeros(256) psnr_max=0 th=0 # Iterate over all possible threshold values with a step size of 5 for t in range(0, 256, 5): # Threshold the image using the current threshold value _, binary = cv2.threshold(img, t, 255, cv2.THRESH_BINARY) # Calculate the PSNR between the binary image and the original image psnr = cv2.PSNR(binary, img) # Store the PSNR value psnr_values[t] = psnr if (psnr_max threshval, self.grayImg, 0) # return binaryImg # def manually(self,threshval): # binaryImg = np.zeros_like(self.grayImg) # for i in range(self.grayImg.shape[0]): #height # for j in range(self.grayImg.shape[1]): #width # if self.grayImg[i, j] < threshval: # binaryImg[i, j] = 0 # else: # binaryImg[i, j] = 1 # return binaryImg # def pirahansiah_threshold_method_find_threshold_values_2(self): # #http://www.jatit.org/volumes/Vol95No21/1Vol95No21.pdf # #https://pdfs.semanticscholar.org/05b2/d39fce4e8a99897e95f8c75416f65a5a0acc.pdf # assert self.grayImg is not None, "file could not be read, check with os.path.exists()" # #img = cv2.GaussianBlur(self.grayImg, (3, 3), 0) # img = self.grayImg # # Initialize an array to store the PSNR values for each threshold value # psnr_values = np.zeros(256) # psnr_max=0 # th=0 # # Iterate over all possible threshold values with a step size of 5 # for t in range(0, 256, 5): # # Threshold the image using the current threshold value # _, binary = cv2.threshold(img, t, 255, cv2.THRESH_BINARY) # # Calculate the PSNR between the binary image and the original image # psnr = cv2.PSNR(binary, img) # # Store the PSNR value # psnr_values[t] = psnr # if (psnr_max threshval); #thresholding #opencv #python The PirahanSiah’s method for thresholding, described in the paper, uses a gray-scale histogram, thresholding range, and the Peak Signal-to-Noise Ratio (PSNR) to segment images and find the best threshold values to binarize the image. They argue that thresholding is an important problem in pattern recognition and use the PSNR quality measure to assess the similarities between the original and binarized image. They calculate PSNRs for every threshold value and use the difference between the PSNR of the previous threshold image and the new one to select the threshold value. They also describe a multi-threshold algorithm that applies multiple threshold values and computes the total number of blobs or objects in an image for each threshold. The peak threshold values are those with the highest total number of blobs compared to their threshold neighbors. In addition, their method uses thresholding on images suitable for OCR systems, LPR systems, etc. The proposed adaptive threshold method, based on the Peak Signal-to-Noise Ratio (PSNR), has the potential to be applied in all domains, such as LPR and OCR. The proposed algorithm achieves competitive results in four databases, including Malaysian vehicle, standard, printed and handwritten images. The objective of this research was to develop a new single adaptive thresholding algorithm that works for a wide range of pattern recognition applications. The proposed method has been implemented in four different types of applications and compared with other methods. The results show that the proposed algorithm achieves the objective because it has obtained reasonable results in all four areas/domains. https://www.jatit.org/volumes/Vol57No2/4Vol57No2.pdf http://www.jatit.org/volumes/Vol95No21/1Vol95No21.pdf https://pdfs.semanticscholar.org/05b2/d39fce4e8a99897e95f8c75416f65a5a0acc.pdf '''