def show_gray_histogram(self): numbins = 256 ranges = [0.0, 255.0] width = 256 height = 256 bytes_per_line = 3 * width hist_image = np.zeros([height, width, 3], np.uint8) # hist_image = np.zeros((256,256,3)) #创建用于绘制直方图的全0图像 bins = np.arange(numbins).reshape(numbins, 1) # 直方图中各bin的顶点位置 color = [(255, 0, 0)] # BGR三种颜色 for ch, col in enumerate(color): origin_hist = cv2.calcHist([self.gray_image], [ch], None, [numbins], ranges) cv2.normalize(origin_hist, origin_hist, 0, 255 * 0.9, cv2.NORM_MINMAX) hist = np.int32(np.around(origin_hist)) pts = np.column_stack((bins, hist)) cv2.polylines(hist_image, [pts], False, col) # print(type(hist_image.data)) hist_image = np.flipud(hist_image) # cv2.imshow("histogram", hist_image) demo_utils.show_cvimage_to_label(hist_image, self.gray_histogram_label)
def preprocessing_algorithm(img_path, sigmaX=10, interpolation=None): """ borrow from: https://www.kaggle.com/ratthachat/aptos-updatedv14-preprocessing-ben-s-cropping/comments :param interpolation: :param img_path: :param sigmaX: :return: """ def _crop_image_from_gray(img, tol=7): import numpy as np if img.ndim == 2: mask = img > tol return img[np.ix_(mask.any(1), mask.any(0))] elif img.ndim == 3: gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) mask = gray_img > tol check_shape = img[:, :, 0][np.ix_(mask.any(1), mask.any(0))].shape[0] if check_shape == 0: # image is too dark so that we crop out everything, return img # return original image else: img1 = img[:, :, 0][np.ix_(mask.any(1), mask.any(0))] img2 = img[:, :, 1][np.ix_(mask.any(1), mask.any(0))] img3 = img[:, :, 2][np.ix_(mask.any(1), mask.any(0))] # print(img1.shape,img2.shape,img3.shape) img = np.stack([img1, img2, img3], axis=-1) # print(img.shape) return img def _standardize(image_array): arr = np.asarray(image_array, dtype='float64') mean, std = arr.mean(), arr.std() return ((arr - mean) / std).astype('float32') def _clahe_rgb(rgb_array, clip_limit=2.0, tile_grid_size=(8, 8)): # convert RGB to LAB lab = cv2.cvtColor(rgb_array, cv2.COLOR_RGB2LAB) # apply clahe on LAB's L component. lab_planes = cv2.split(lab) clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=tile_grid_size) lab_planes[0] = clahe.apply(lab_planes[0]) lab = cv2.merge(lab_planes) # remap LAB tp RGB. rgb = cv2.cvtColor(lab, cv2.COLOR_LAB2RGB) return rgb image = cv2.imread(img_path) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = _crop_image_from_gray(image) cv2.normalize(src=image, dst=image, alpha=255, norm_type=cv2.NORM_INF) image = cv2.resize(image, tuple(IMAGE_SHAPE[:-1]), interpolation) # image = _clahe_rgb(image, clip_limit=5, tile_grid_size=(4,4)) # image = cv2.addWeighted(image, 4, cv2.GaussianBlur(image, (0, 0), sigmaX), -4, 128)# , cv2.INTER_LANCZOS4 return image
def imageBGR2FlatHist(image, bins=(8, 8, 8)): hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) hist = cv2.calcHist([hsv], [0, 1, 2], None, bins, [0, 180, 0, 256, 0, 256]) if imutils.is_cv2(): hist = cv2.normalize(hist) else: cv2.normalize(hist, hist) return hist.flatten()
def back_projection_demo(target): sample = target[430:442, 305:317, :3] # 建筑墙面色调块 roi_hsv = cv.cvtColor(sample, cv.COLOR_BGR2HSV) target_hsv = cv.cvtColor(target, cv.COLOR_BGR2HSV) cv.imshow("sample", sample) cv.imshow("target", target) roihist = cv.calcHist([roi_hsv], [0, 1], None, [8, 8], [0, 180, 0, 256]) # [180, 256] binsize小则查找粗糙 cv.normalize(roihist, roihist, 0, 255, cv.NORM_MINMAX) # 归一化 dst = cv.calcBackProject([target_hsv], [0, 1], roihist, [0, 180, 0, 256], 1) cv.imshow("backproject", dst)
def histogram(self, image, mask): # extract a 3D color histogram from the masked region of the # image, using the supplied number of bins per channel hist = cv2.calcHist([image], [0, 1, 2], mask, self.bins, [0, 180, 0, 256, 0, 256]) # normalize the histogram if we are using OpenCV 2.4 if imutils.is_cv2(): hist = cv2.normalize(hist).flatten() # otherwise handle for OpenCV 3+ else: hist = cv2.normalize(hist, hist).flatten() # return the histogram return hist
def hist_lines(im): h = np.zeros((300, 256, 3)) if len(im.shape) != 2: print("hist_lines applicable only for grayscale images") #print("so converting image to grayscale for representation" im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) hist_item = cv2.calcHist([im], [0], None, [256], [0, 256]) cv2.normalize(hist_item, hist_item, 0, 255, cv2.NORM_MINMAX) hist = np.int32(np.around(hist_item)) for x, y in enumerate(hist): cv2.line(h, (x, 0), (x, y), (255, 255, 255)) y = np.flipud(h) return y
def back_projection_demo():#反向投影 roi = cv.imread("C:/Users/32936/Desktop/2/qiuyi.jpg")#搜索目标 target = cv.imread("C:/Users/32936/Desktop/2/maidi.jpg")#待搜索图片 hsv = cv.cvtColor(roi,cv.COLOR_BGR2HSV) hsvt = cv.cvtColor(target,cv.COLOR_BGR2HSV) roihist = cv.calcHist([hsv],[0,1],None,[32,32],[0,180,0,256])#获取搜索目标直方图 cv.normalize(roihist,roihist,0,255,cv.NORM_MINMAX)#直方图归一化 mask = cv.calcBackProject([hsvt],[0,1],roihist,[0,180,0,256],3)#直方图反向投影 dst = cv.bitwise_and(target,target,mask=mask) cv.imshow("result",dst) cv.imshow("target",target) cv.imshow("image",roi) cv.imshow("mask",mask)
def hist_curve(im): h = np.zeros((300, 256, 3)) if len(im.shape) == 2: color = [(255, 255, 255)] elif im.shape[2] == 3: color = [(255, 0, 0), (0, 255, 0), (0, 0, 255)] for ch, col in enumerate(color): hist_item = cv2.calcHist([im], [ch], None, [256], [0, 256]) cv2.normalize(hist_item, hist_item, 0, 255, cv2.NORM_MINMAX) hist = np.int32(np.around(hist_item)) pts = np.int32(np.column_stack((bins, hist))) cv2.polylines(h, [pts], False, col) y = np.flipud(h) return y
def hist_diff(img_1, img_2): """ Calculates distance between images based on the distance between their histograms. Example found at: https://www.pyimagesearch.com/2014/07/14/3-ways-compare-histograms-using-opencv-python/ """ hist_1 = cv2.calcHist([img_1], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256]) hist_1 = cv2.normalize(hist_1, hist_1).flatten() hist_2 = cv2.calcHist([img_2], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256]) hist_2 = cv2.normalize(hist_2, hist_2).flatten() return cv2.compareHist(hist_1, hist_2, cv2.HISTCMP_BHATTACHARYYA)
def watershed_demo(image): blurred = cv.pyrMeanShiftFiltering(image, 10, 100) gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY) ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) cv.imshow("binary", binary) kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3)) mb = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel, iterations=2) sure_bg = cv.dilate(binary, kernel, iterations=3) cv.imshow("mor", sure_bg) dist = cv.distanceTransform(mb, cv.DIST_L2, 3) dist_output = cv.normalize(dist, 0, 1.0, cv.NORM_MINMAX) cv.imshow("dist", dist_output * 50) ret, surface = cv.threshold(dist, dist.max() * 0.6, 255, cv.THRESH_BINARY) cv.imshow("interface", surface) surface_fg = np.uint8(surface) unknow = cv.subtract(sure_bg, surface_fg) ret, markers = cv.connectedComponents(surface_fg) print(ret) markers += 1 markers[unknow == 255] = 0 markers = cv.watershed(image, markers=markers) image[markers == -1] = [0, 0, 255] cv.imshow("result", image)
def aug(src): """图像亮度增强""" if get_lightness(src) > 130: print("图片亮度足够,不做增强") # 先计算分位点,去掉像素值中少数异常值,这个分位点可以自己配置。 # 比如1中直方图的红色在0到255上都有值,但是实际上像素值主要在0到20内。 max_percentile_pixel, min_percentile_pixel = compute(src, 1, 99) # 去掉分位值区间之外的值 src[src >= max_percentile_pixel] = max_percentile_pixel src[src <= min_percentile_pixel] = min_percentile_pixel # 将分位值区间拉伸到0到255,这里取了255*0.1与255*0.9是因为可能会出现像素值溢出的情况,所以最好不要设置为0到255。 out = np.zeros(src.shape, src.dtype) cv2.normalize(src, out, 255 * 0.1, 255 * 0.9, cv2.NORM_MINMAX) return out
def get_features(imgs, c): all_images = [] for image_input in imgs: tflib.init_tf() image = cv2.imread(str(image_input)) image = cv2.normalize(image, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) image = cv2.resize(image, (256, 256), interpolation=cv2.INTER_AREA) image = np.array(image) image = np.expand_dims(image.T, axis=0) all_images.append(image) concat_imgs = tf.concat(all_images, 0, name='concat') logits = c.get_output_for(concat_imgs, None, is_validation=True, randomize_noise=True) predictions = [tf.nn.softmax(tf.concat([logits, -logits], axis=1))] result = tflib.run(predictions)[0].tolist() # return logits return result[0]
def get_frame(self): success, image = self.video.read() cv2.normalize(image, image, (0+exposure + contrast), (255 + exposure - contrast), cv2.NORM_MINMAX) global cameraCrop spec = normalise(getSpectrum(image[cameraCrop[0][1] : cameraCrop[1][1], cameraCrop[0][0]: cameraCrop[1][0]])) global spectrum spectrum = spec image = cv2.rectangle(image, (cameraCrop[0][0], cameraCrop[0][1]), (cameraCrop[1][0], cameraCrop[1][1]), (0,0, 255), 3) ret, jpeg = cv2.imencode('.png', image[:, :]) return jpeg.tobytes()
def Normalize(img, Norm=True, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]): cv2.normalize(img, img, 0, 255, cv2.NORM_MINMAX) img = img / 255. # img = img.astype(np.float32) # if np.max(img) - np.min(img) > 0.001: # img = (img-np.min(img)) / (np.max(img)-np.min(img)) # else: # img = img - np.min(img) if Norm: level = len(img.shape) assert level <= 3, 'level should <= 3' if level == 3: for i in range(level): img[:, :, i] = (img[:, :, i] - mean[i]) / std[i] else: img[:, :] = (img[:, :] - mean[0]) / std[0] return img
def arr2heatmap(arr): heatmap = cv2.normalize(arr, dst=None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U) heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET) return heatmap
def motion_blur(gray, degree, angle): gray = np.array(gray) M = cv2.getRotationMatrix2D((round(degree / 2), round(degree / 2)), angle, 1) motion_blur_kernel = np.diag(np.ones(degree)) motion_blur_kernel = cv2.warpAffine(motion_blur_kernel, M, (degree, degree)) PSF = motion_blur_kernel / degree blurred = cv2.filter2D(gray, -1, PSF) blurred = cv2.normalize(blurred,None, 0, 255, cv2.NORM_MINMAX) blurred = np.array(blurred, dtype=np.uint8) return blurred,PSF
def main(): drone = tellopy.Tello() try: drone.connect() drone.wait_for_connection(60.0) pygame.init() pygame.joystick.init() container = av.open(drone.get_video_stream()) frameCount = 0 # Stores the current frame being processed frame1 = None # Store variables for first frame frame2 = None # Store variables for second frame prvs = None hsv = None while True: for frame in container.decode(video=0): checkController() frameCount += 1 if frameCount == 1: # If first frame frame1 = cv2.cvtColor(np.array(frame.to_image()), cv2.COLOR_RGB2BGR) prvs = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) hsv = np.zeros_like(frame1) hsv[..., 1] = 255 else: # If not first frame frame2 = cv2.cvtColor(np.array(frame.to_image()), cv2.COLOR_RGB2BGR) next = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY) flow = cv2.calcOpticalFlowFarneback( prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0) mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) hsv[..., 0] = ang * 180 / np.pi / 2 hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) cv2.imshow('frame2', bgr) k = cv2.waitKey(30) & 0xff if k == 27: break elif k == ord('s'): cv2.imwrite('opticalfb.png', frame2) cv2.imwrite('opticalhsv.png', bgr) prvs = next print(frameCount) except Exception as ex: exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback) print(ex) finally: drone.quit() cv2.destroyAllWindows()
def ssr_c(img, size): img_G = replaceZeroes(cv2.GaussianBlur(img, (size, size), 0)) img = replaceZeroes(img) # img_G = cv2.GaussianBlur(img, (size, size), 0) log_S = cv2.log(img / 255.0) g_L = cv2.log(img_G / 255.0) log_L = cv2.multiply(log_S, g_L) log_R = cv2.subtract(log_S, log_L) dst_R = cv2.normalize(log_R, None, 0, 255, cv2.NORM_MINMAX) R_c = cv2.convertScaleAbs(dst_R) return R_c
def image_to_tensor(image): """ receives image and converts it to tensor """ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA) image = np.asarray(image) image = cv2.normalize(image.astype('float'), None, 0, 1, cv2.NORM_MINMAX) image = np.expand_dims(image, axis=0) return image
def data2img(data, normalization=1): # normalization, type castinf(uint8). gray2rgb if normalization == 1: data = cv2.normalize(src=data, dst=None, beta=0, alpha=255, norm_type=cv2.NORM_MINMAX) data = np.uint8(data) if len(data.shape) == 2: data = np.stack((data, ) * 3, axis=2) return data
def _normalise_img(img): """Replaces the given image with a new image of the same shape, but linearly scaled such that all points are between 0 and 1 """ img = cv2.normalize( src=img, dst=img, alpha=0, beta=1.0, norm_type=cv2.NORM_MINMAX, ) return img
def _spacial_diff(self, img, dx=1, dy=1): ''' ### Now deprecated ! ### Get spacial difference image with offset dx and dy. ''' H = np.zeros((2, 3)) H[0, 0] = 1 H[0, 2] = dx H[1, 1] = 1 H[1, 2] = dy translated = cv.warpAffine(img, H, (img.shape[1], img.shape[0])) img = translated - img img = cv.normalize(img, None, 0, 255, cv.NORM_MINMAX) return np.uint8(img)
def optical_flow(img: np.ndarray, previous: np.ndarray) -> np.ndarray: mask = np.zeros(shape=img.shape, dtype=np.uint8) mask[..., 1] = 255 gray = to_gray(img) previous = to_gray(previous) flow = cv2.calcOpticalFlowFarneback(previous, gray, None, 0.5, 3, 15, 3, 5, 1.2, 0) magnitude, polar_angle = cv2.cartToPolar(flow[..., 0], flow[..., 1]) mask[..., 0] = polar_angle * 180 / np.pi / 2 mask[..., 2] = cv2.normalize(magnitude, None, 0, 255, cv2.NORM_MINMAX) rgb = cv2.cvtColor(mask, cv2.COLOR_HSV2BGR) return rgb
def optical_flow(previous_, current_): """ 光流 :param previous_: :param current_: :return: """ previous = cv2.cvtColor(previous_, cv2.COLOR_BGR2GRAY) current = cv2.cvtColor(current_, cv2.COLOR_BGR2GRAY) flow = cv2.calcOpticalFlowFarneback(previous, current, None, 0.5, 3, 15, 3, 5, 1.2, 0) hsv = np.zeros_like(previous_) mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) hsv[..., 0] = ang * 180 / np.pi / 2 hsv[..., 1] = 255 hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) flow = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) return flow
def Shadow_removal(warped): # Funkcija za otklanjanje sjene sa slike img = warped.copy() # Kopiramo poravnatu sliku pod imenom img rgb_planes = cv2.split(img) # Razdvajamo boje RGB spektruma result_norm_planes = [] # Niz u koji spremamo normalizirane boje slike # Prolazimo kroz sve dijelove boja slike for plane in rgb_planes: dilated_img = cv2.dilate(plane, np.ones((7,7), np.uint8)) # bg_img = cv2.medianBlur(dilated_img, 21) # Prolazimo kroz sve 3 boje RGB-a, te diff_img = 255 - cv2.absdiff(plane, bg_img) # Normaliziramo svaku boju kako bi otklonili sjenu norm_img = cv2.normalize(diff_img,None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1) # result_norm_planes.append(norm_img) # Spremamo narmaliziranu boju u niz result_norm = cv2.merge(result_norm_planes) # Ponovno spajamo sve boje u jednu sliku result_norm = cv2.cvtColor(result_norm, cv2.COLOR_BGR2GRAY) # Pretvaramo sliku u crno-bijelu cv2.imwrite('scanned.jpg', result_norm) # Spremamo sliku kojoj smo otklonili sjenu
def describe(image): ''' def calcHist(images, channels, mask, histSize, ranges, hist=None, accumulate=None) images : image array in a list channels = R,G,B - 0,1,256 mask not required - 8bit array histSize = number of divisions per channel in histogram (8 Reds , 8 Blues , 8 Greens) all data is sorted into this (8,8,8) is default - changing value will split data into more sections making it less effective ranges = range of each color value - lowest = 0 ( BLACK ) highest = 255 ( WHITE ) so 255 + 1 hist = not sure what this does default is none accumulate will pool all data on top of each other - leave it as none ''' hist = cv2.calcHist([image], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256]) hist = cv2.normalize(hist, hist) return hist.flatten()
def apply_histogram(img_n): img_n = np.array(img_n) newImg = [] for i in img_n: img = cv2.normalize(src=i, dst=None, alpha=0, beta=80, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U) equ = cv2.equalizeHist(img) # res = np.hstack((img, adjusted, equ)) newImg.append(equ.tolist()) return newImg
def predImages(imgs, path): pred = [] i = 0 print("[PYTHON]: Processing images....") for til in tqdm(imgs): prediction = model.predict(np.expand_dims(til, axis=0))[0] cv2.cvtColor(prediction, cv2.CV_8UC1, 255) prediction_norm = cv2.normalize(prediction, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) np.array(prediction_norm).astype(int) cv2.imwrite(path + str(i) + "pred.png", prediction_norm) pred.append(prediction_norm) i = i + 1 return pred
def Dense_Optical_Flow(image): image_old = image frame1 = image next = image hsv = np.zeros([frame1.shape[0], frame1.shape[1], 3], dtype=np.uint8) hsv[..., 1] = 255 while (1): flow = cv2.calcOpticalFlowFarneback(image_old, next, None, 0.5, 3, 50, 3, 5, 1.2, 0) mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) hsv[..., 0] = ang * 180 / np.pi / 2 hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) image_old = next k = cv2.waitKey(30) & 0xff if k == 27: break next = (yield bgr)
def imageCompress(comp_val): global img_n global save_img input_img = cv2.imread(image_locs[curr]) input_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2RGB) img_data = (input_img / 255.0).reshape(-1, 3) kmeans = MiniBatchKMeans(comp_val).fit(img_data) k_colors = kmeans.cluster_centers_[kmeans.predict(img_data)] k_img = reshape(k_colors, (input_img.shape)) img_data = (k_img * 255.0) img_n = cv2.normalize(src=img_data, dst=None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U) img_n = Image.fromarray(img_n) save_img = img_n convertToTkimg(img_n)