def cartoon(self, image): ''' Cartoonise Image! Datatypes: image:nparray format BGR ''' numdown, numbilateral = 2, 7 color = image for _ in range(numdown): color = cv2.pyrDown(color) for _ in range(numbilateral): color = cv2.bilateralFilter(color, d=9, sigmaColor=9, sigmaSpace=7) for _ in range(numdown): color = cv2.pyrUp(color) cartoon = cv2.bitwise_and( color, cv2.cvtColor( cv2.adaptiveThreshold(cv2.medianBlur( cv2.cvtColor(image, cv2.COLOR_RGB2GRAY), 7), 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, blockSize=9, C=2), cv2.COLOR_GRAY2RGB)) cartoon = cv2.cvtColor(cartoon, cv2.COLOR_BGR2RGB) return cartoon
def pyramid_demo(image): level = 3 #设置金字塔的层数为3 temp = image.copy() #拷贝图像 pyramid_images = [] #建立一个空列表 for i in range(level): dst = cv.pyrDown(temp) #先对图像进行高斯平滑,然后再进行降采样(将图像尺寸行和列方向缩减一半) pyramid_images.append(dst) #在列表末尾添加新的对象 cv.imshow("pyramid" + str(i), dst) temp = dst.copy() return pyramid_images
def gauss_pyramid_demo(image): level = 3 temp = image.copy() pyramid_image = [] for i in range(level): dst = cv.pyrDown(temp) pyramid_image.append(dst) cv.imshow("pyramid" + np.str(i), dst) temp = dst.copy() return pyramid_image
def toCarttonStyle(picturePath): # 设置输入输出路径和文件名称 imgInput_FileName = picturePath imgOutput_FileName = 'res.jpg' # 属性设置 num_down = 2 # 缩减像素采样的数目 num_bilateral = 7 # 定义双边滤波的数目 # 读取图片 img_rgb = cv2.imread(imgInput_FileName) # 用高斯金字塔降低取样 img_color = img_rgb for _ in range(num_down): img_color = cv2.pyrDown(img_color) # 重复使用小的双边滤波代替一个大的滤波 for _ in range(num_bilateral): img_color = cv2.bilateralFilter(img_color, d=9, sigmaColor=9, sigmaSpace=7) # 升采样图片到原始大小 for _ in range(num_down): img_color = cv2.pyrUp(img_color) # 转换为灰度并且使其产生中等的模糊 img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY) img_blur = cv2.medianBlur(img_gray, 7) # 检测到边缘并且增强其效果 img_edge = cv2.adaptiveThreshold(img_blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, blockSize=9, C=2) # 算法处理后,照片的尺寸可能会不统一 # 把照片的尺寸统一化 height=img_rgb.shape[0] width = img_rgb.shape[1] img_color=cv2.resize(img_color,(width,height)) # 转换回彩色图像 img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB) img_cartoon = cv2.bitwise_and(img_color, img_edge) # 保存转换后的图片 cv2.imwrite(imgOutput_FileName, img_cartoon) print('文件转换成漫画成功,保存在' + imgOutput_FileName)
def cartoonizer(img, num_down=2, num_bi=5): #Params #num_down = 2 #DOWNSAMPLE STEPS #num_bi = 5 # BILATERAL FILTERING STEPS img_c = img for ix in range(num_down): img_c = cv2.pyrDown(img) # Pyramid Down : Downsampling # print(img_c.shape) for iy in range(num_bi): img_c = cv2.bilateralFilter(img_c, d=9, sigmaColor=9, sigmaSpace=7) #Filtering # print(img_c.shape) #UPSAMPLING for ix in range(num_down): img_c = cv2.pyrUp(img_c) # Pyramid Down : Downsampling # print(img_c.shape) #BLUR and Threshold img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # GRAY SCALE img_blur = cv2.medianBlur(img_gray, 7) #MEDIAN BLUR img_edge = cv2.adaptiveThreshold(img_blur, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, blockSize=9, C=2) img_c = cv2.resize(img_c, (800, 800)) #RGB CONVERSION + BITWISE & img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB) # print(img_c.shape) # print(img_edge.shape) img_cartoon = cv2.bitwise_and(img_c, img_edge) stack = np.hstack([img, img_cartoon]) return stack
from cv2 import cv2 as cv import numpy as np from matplotlib import pyplot as plt #pyrDown() 从一个高分辨率大尺寸的图像向上构建一个金子塔 # (尺寸变小,分辨率降低)。 img = cv.imread('star.jpg') cv.imshow('img', img) lower_reso = cv.pyrDown(img) high_reso = cv.pyrUp(lower_reso) cv.imshow('lower_case', lower_reso) cv.imshow('high_reso', high_reso) key = cv.waitKey(0) if key == 27: cv.destroyAllWindows()
# it's going wrong when add or sub if their sizes are not same # it may change size if pyrDown() then pyrUp(), so resize it from cv2 import cv2 import numpy as np,sys A = cv2.imread('resource/apple.jpg') B = cv2.imread('resource/orange.jpg') # generate Gaussian pyramid for A G = A.copy() gpA = [G] for i in xrange(6): G = cv2.pyrDown(G) # cv2.imshow('gpa'+ str(i), G) gpA.append(G) # generate Gaussian pyramid for B G = B.copy() gpB = [G] for i in xrange(6): G = cv2.pyrDown(G) gpB.append(G) # generate Laplacian Pyramid for A lpA = [gpA[5]] for i in xrange(5,0,-1): GE = cv2.pyrUp(gpA[i]) GE_1 = cv2.resize(gpA[i-1], GE.shape[0:2]) # cv2.imshow('ge'+ str(i), GE_1)
import numpy as np from cv2 import cv2 img = cv2.imread('lena.jpg') layer = img.copy() gp = [layer] for i in range(6): layer = cv2.pyrDown(layer) gp.append(layer) #cv2.imshow(str(i), layer) layer = gp[5] cv2.imshow('upper level Gaussian Pyramid', layer) lp = [layer] for i in range(5, 0, -1): gaussian_extended = cv2.pyrUp(gp[i]) laplacian = cv2.subtract(gp[i - 1], gaussian_extended) cv2.imshow(str(i), laplacian) cv2.imshow('Original Image', img) cv2.waitKey(0) cv2.destroyAllWindows()
from cv2 import cv2 as cv import numpy as np def cv_show(img, name): cv.imshow('tree', img) cv.waitKey(0) cv.destroyAllWindows() img = cv.imread('tree.jpg') up = cv.pyrDown(img) down=cv.pyrUp(img) cv_show(img,'down')
print("Press q to capture and exit") cam = cv2.VideoCapture(0) while True: ret,frame = cam.read() if ret == False: print("Can't access camera, Something went wrong!") continue cv2.imshow("Original",frame) img = cv2.resize(frame,(800,800)) # DOWNSAMPLING + BILATERIAL FILTER img_c = img for ix in range(num_down): img_c = cv2.pyrDown(img)# Pyramid Down : Downsampling # print(img_c.shape) for iy in range(num_bi): img_c = cv2.bilateralFilter(img_c,d=9,sigmaColor=9,sigmaSpace=7) #Filtering # print(img_c.shape) #UPSAMPLING for ix in range(num_down): img_c = cv2.pyrUp(img_c)# Pyramid Down : Downsampling # print(img_c.shape) #BLUR and Threshold img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) # GRAY SCALE img_blur = cv2.medianBlur(img_gray,7) #MEDIAN BLUR img_edge = cv2.adaptiveThreshold(img_blur,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,blockSize=9,C=2) img_c = cv2.resize(img_c,(800,800)) #RGB CONVERSION + BITWISE &
# # cv.imshow("original image", img) # cv.imshow("first down", lr) # cv.imshow("second down", lr1) # cv.imshow("third down", lr2) # cv.imshow("first up", hr) # cv.imshow("second up", hr1) # cv.imshow("third up", hr2) # laplacian pyramids layer = img.copy() gaussian_pyr = [layer] for i in range(6): layer = cv.pyrDown(layer) gaussian_pyr.append(layer) # cv.imshow("{}-down".format(i+1), layer) layer = gaussian_pyr[5] cv.imshow("Upper level Gaussian pyramid", layer) lp = layer for i in range(5, 0, -1): gaussian_expanded = cv.pyrUp(gaussian_pyr[i]) laplacian_pyr = cv.subtract(gaussian_pyr[i-1], gaussian_expanded) cv.imshow("{}-pyramid".format(i), laplacian_pyr) cv.waitKey(0) cv.destroyAllWindows()
from cv2 import cv2 as cv import numpy as np from matplotlib import pyplot as plt tmp_img = cv.imread('star.jpg') cnt = 0 cv.imshow('win', tmp_img) # 向下生成三级高斯金字塔 while (True): cnt += 1 print(cnt) tmp_img = cv.pyrDown(tmp_img) cv.imshow('window', tmp_img) key = cv.waitKey(0) if key == 27 and cnt == 3: break cnt = 0 #向上生成三级高斯金字塔 while (True): cnt += 1 print(cnt) tmp_img = cv.pyrUp(tmp_img) cv.imshow('window', tmp_img) key = cv.waitKey(0) if key == 27 and cnt == 3: break cv.destroyAllWindows()
#find the relative bin areas of botht the model image and the captured image model_areas, filled_image, pixel_count, cX, cY = find_bin_percentages( expected_model_image) model_segmented = display(filled_image, pixel_count, model_areas, cX, cY) real_areas, filled_image, pixel_count, cX, cY = find_bin_percentages( captured_image) real_segmented = display(filled_image, pixel_count, real_areas, cX, cY) #find the area discrepancy between the model and the captured image difference = find_discrepancy(model_areas, real_areas) #go down pyramid until potential match is found for images that may not have been detected properly #doesn't matter that it amplifies errors for already false images, it's trying to find correct shapes. if difference > 7: print("layer 1") captured_pyr_level_1 = cv2.pyrDown(captured_image) pyr_areas, filled_image, pixel_count, cX, cY = find_bin_percentages( captured_pyr_level_1) pyr_segmented = display(filled_image, pixel_count, pyr_areas, cX, cY) difference = find_discrepancy(model_areas, pyr_areas) if difference > 7: print("layer 2") captured_pyr_level_2 = cv2.pyrDown(captured_pyr_level_1) pyr_areas, filled_image, pixel_count, cX, cY = find_bin_percentages( captured_pyr_level_2) pyr_segmented = display(filled_image, pixel_count, real_areas, cX, cY) difference = find_discrepancy(model_areas, pyr_areas) if (difference > 7): print("layer 3") print("Error detected. Area difference of: " + str(difference) +
def resize_image(image): # nếu resize rồi không cần resize nữa image = cv2.pyrDown(image) image = cv2.pyrDown(image) return image
img, img_bin = convert_image(img) img_final_bin = edit_img(80, 1, 1, 1, 1, 1, 1, img_bin) contours, boundingBoxes = find_contours(img_final_bin, False) no1x, no1y, no1w, no1h, no2x, no2y, no2w, no2h = rotapi.check_angle(contours) piece_img = img[no2y:no2y + no2h, no2x:no2x + no2w] cv2.imwrite(file_path_name, piece_img) cv2.destroyAllWindows() # 이미지 새로 읽어서 테이블 수평 맞춰 주고 저장한다. img = cv2.imread(file_path_name) piece_img = rotapi.rotate_auto(img) cv2.imwrite(file_path_name, piece_img) # 저장한 이미지를 새로 읽는데 너무 커서 사이즈를 축소 시킨다. 웹에서 사용자 입력받는다면 필요 없다. piece_img = cv2.imread(file_path_name, 0) img = cv2.pyrDown(piece_img) img = cv2.pyrDown(img) #img = piece_img origin_img = img # 이미지 재처리 해서 화면에 띄운다. img, img_bin = convert_image(img) img_final_bin = edit_img(80, 1, 1, 1, 1, 1, 1, img_bin) dst = cv2.addWeighted(img, 0.4, img_final_bin, 0.6, 0) # 재처리된 이미지를 디비와 비교하여 같거나 비슷한 템플릿이 있는지 찾아 본다. # 찾아서 있다면 바로 아래 연산 건너뛴다. # 위 연산 결과가 없으면 이미지에 사용자 입력 받아서 쓴다. drawing = False mode = True
def get_deep(image): """ Extracts deep learning model featurs. Both resnet model trained using triplet loss for global feature extraction and scalenet model for scale invariant features. input: takes an image returns resnet feature vector (size 512) and scale model feature vector (size 1024) """ # res18 triplet global features extraction img1 = resize(image, (227, 227, 3)) img1 = torch.FloatTensor(img1).permute(2, 0, 1) transform = transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] ) inp = torch.unsqueeze(transform(img1), 0).to(device) deeprank_ft = deeprank_model.forward_one(inp).cpu().detach().numpy().ravel() # scalenet pyramid pooled features transform_pyr = transforms.Compose( [ transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ] ) G = image.copy() if min(G.shape[:2]) < 64: G = min_64_resize(G) imG = Image.fromarray(G) imG = transform_pyr(imG) im_pyramid = [imG] # creating image pyramid c = 0 while c < 5: G = cv2.pyrDown(G) shape = G.shape[:2] if min(shape) < 64: G = min_64_resize(G) imG = Image.fromarray(G) imG = transform_pyr(imG) im_pyramid.append(imG) break else: imG = Image.fromarray(G) imG = transform_pyr(imG) im_pyramid.append(imG) c += 1 # extracting features of each image in the pyramid and average pooling them scale_ft = [] for scaled_candidate in im_pyramid: scaled_candidate = scaled_candidate.unsqueeze(0).to(device) try: sc_ft = scalenet_model.forward_one(scaled_candidate) except: sc_ft = torch.zeros((1, 1024)) scale_ft.append(sc_ft) if len(scale_ft) > 1: scale_feat = torch.stack(scale_ft) scale_feat = torch.mean(scale_feat, dim=0) # average pooling the features else: scale_feat = scale_ft[0] scalenet_ft = scale_feat.cpu().detach().numpy().ravel() return deeprank_ft, scalenet_ft
def cartoonifier(imagepath): #TO DO #step 1 #Use bilateral filter for edge-aware smoothing. num_down = 1 # number of downsampling steps num_bilateral = 4 # number of bilateral filtering steps img = cv2.imread(imagepath) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # downsample image using Gaussian pyramid(see opencv 'pyrDown()' function) for i in range(num_down): img = cv2.pyrDown(img) # repeatedly apply small bilateral filter instead of # applying one large filter for i in range(num_bilateral): img = cv2.bilateralFilter(img, 15, 75, 75) # upsample image to original size (see opencv 'pyrUp()' function) for i in range(num_down): bilateralImg = cv2.pyrUp(img) #plt.imshow(bilateralImg) # ## Step#2 # ### In this step we will blur the original image. This is considered as a pre-processing step before we move on towards the edge detection step. We will apply a median filter on the image, which replaces each pixel value with the median value of all the pixels in a small neighborhood. # In[5]: #TO DO #step 2 # convert to grayscale and apply median blur img = cv2.imread(imagepath) grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) medianBlurImg = cv2.medianBlur(grayImg, 5) #plt.imshow(medianBlurImg, cmap = 'gray') # ## Step#3 # # ### In this step we will create an edge mask from the output produced in step#2 using adaptive thresholding # In[14]: #TO DO #step 3 # detect and enhance edges(see opencv 'adaptiveThreshold()' function) edgeMask = cv2.adaptiveThreshold( medianBlurImg, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2, ) #plt.imshow(edgeMask, cmap = 'gray') # ## Final Step # # ### In this step we will combine the output produced in step#1 and step#3 using a bitwise and operator to produce our final output.(Note: You need to convert output from step#3 to color first) # In[40]: #TO DO #Final Step # convert back to color, bit-AND with color image finalMask = cv2.cvtColor(edgeMask, cv2.COLOR_GRAY2RGB) (x1, y1, z1) = bilateralImg.shape (x2, y2, z2) = finalMask.shape if x1 > x2: bilateralImg = np.delete(bilateralImg, x1 - 1, 0) elif x2 > x1: finalMask = np.delete(finalMask, x2 - 1, 0) if y1 > y2: bilateralImg = np.delete(bilateralImg, y1 - 1, 1) elif y2 > y1: finalMask = np.delete(finalMask, y2 - 1, 1) finalImg = cv2.bitwise_and(bilateralImg, finalMask, mask=None) return finalImg
# coding: utf-8 import numpy as np from cv2 import cv2 import matplotlib.pyplot as plt img = cv2.imread(r'pictures\a.05x.jpg') higher_reso = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) lower_reso = cv2.pyrDown(higher_reso) higher_reso2 = cv2.pyrUp(lower_reso) # Normally, we used to work with an image of constant size. But in some occassions, we need to work with images of different resolution of the same image. For example, while searching for something in an image, like face, we are not sure at what size the object will be present in the image. In that case, we will need to create a set of images with different resolution and search for object in all the images. These set of images with different resolution are called Image Pyramids (because when they are kept in a stack with biggest image at bottom and smallest image at top look like a pyramid). # There are two kinds of Image Pyramids. 1) Gaussian Pyramid and 2) Laplacian Pyramids # Higher level (Low resolution) in a Gaussian Pyramid is formed by removing consecutive rows and columns in Lower level (higher resolution) image. Then each pixel in higher level is formed by the contribution from 5 pixels in underlying level with gaussian weights. By doing so, a M \times N image becomes M/2 \times N/2 image. So area reduces to one-fourth of original area. It is called an Octave. The same pattern continues as we go upper in pyramid (ie, resolution decreases). Similarly while expanding, area becomes 4 times in each level. We can find Gaussian pyramids using cv2.pyrDown() and cv2.pyrUp() functions. # Laplacian Pyramids are formed from the Gaussian Pyramids. There is no exclusive function for that. Laplacian pyramid images are like edge images only. Most of its elements are zeros. They are used in image compression. A level in Laplacian Pyramid is formed by the difference between that level in Gaussian Pyramid and expanded version of its upper level in Gaussian Pyramid. plt.subplot(2, 2, 1), plt.imshow(higher_reso, cmap='gray') plt.title('Original'), plt.xticks([]), plt.yticks([]) plt.subplot(2, 2, 2), plt.imshow(lower_reso, cmap='gray') plt.title('lower_reso'), plt.xticks([]), plt.yticks([]) plt.subplot(2, 2, 3), plt.imshow(higher_reso2, cmap='gray') plt.title('higher_reso2'), plt.xticks([]), plt.yticks([]) plt.show()
import numpy as np from cv2 import cv2 apple = cv2.imread('apple.jpg') orange = cv2.imread('orange.jpg') print(apple.shape) print(orange.shape) apple_orange = np.hstack((apple[:, :256], orange[:, 256:])) # generate gaussian pyramid for apple apple_copy = apple.copy() gp_apple = [apple_copy] for i in range(6): apple_copy = cv2.pyrDown(apple_copy) gp_apple.append(apple_copy) #cv2.imshow(str(i), apple_copy) # generate gaussian pyramid for orange orange_copy = orange.copy() gp_orange = [orange_copy] for i in range(6): orange_copy = cv2.pyrDown(orange_copy) gp_orange.append(orange_copy) #cv2.imshow(str(i), orange_copy) # generate laplacian pyramid for apple apple_copy = gp_apple[5] lp_apple = [apple_copy]
import cv2.cv2 as cv import numpy as np apple = cv.imread("imgs/apple.jpg") orange = cv.imread("imgs/orange.jpg") print(apple.shape, orange.shape) apple_orange = np.hstack((apple[:, :256], orange[:, 256:])) # gaussian pyramid apple apple_c = apple.copy() gp_apple = [apple_c] for i in range(6): apple_c = cv.pyrDown(apple_c) gp_apple.append(apple_c) # gaussian pyramid orange orange_c = orange.copy() gp_orange = [orange_c] for i in range(6): orange_c = cv.pyrDown(orange_c) gp_orange.append(orange_c) # laplacian pyramid apple apple_c = gp_apple[5] lp_apple = [apple_c] for i in range(5, 0, -1): gaussian_expanded = cv.pyrUp(gp_apple[i]) laplacian = cv.subtract(gp_apple[i-1], gaussian_expanded) lp_apple.append(laplacian)
def imagePyramidImg(self): imageFirst = Image.open(self.filename) imageLast = imageFirst.resize((450, 450), Image.ANTIALIAS) imageLast.save('img/dist/temp1.jpg') imageFirst2 = Image.open(self.filename2) imageLast2 = imageFirst2.resize((450, 450), Image.ANTIALIAS) imageLast2.save('img/dist/temp2.jpg') A = cv2.imread('img/dist/temp1.jpg') B = cv2.imread('img/dist/temp2.jpg') G = A.copy() gpA = [G] for i in range(6): G = cv2.pyrDown(G) gpA.append(G) G = B.copy() gpB = [G] for i in range(6): G = cv2.pyrDown(G) gpB.append(G) lpA = [gpA[5]] for i in range(6, 0, -1): GE = cv2.pyrUp(gpA[i]) GE = cv2.resize(GE, gpA[i - 1].shape[-2::-1]) L = cv2.subtract(gpA[i - 1], GE) lpA.append(L) lpB = [gpB[5]] for i in range(6, 0, -1): GE = cv2.pyrUp(gpB[i]) GE = cv2.resize(GE, gpB[i - 1].shape[-2::-1]) L = cv2.subtract(gpB[i - 1], GE) lpB.append(L) LS = [] lpAc = [] for i in range(len(lpA)): b = cv2.resize(lpA[i], lpB[i].shape[-2::-1]) lpAc.append(b) j = 0 for i in zip(lpAc, lpB): la, lb = i rows, cols, dpt = la.shape ls = np.hstack((la[:, 0:cols // 2], lb[:, cols // 2:])) j = j + 1 LS.append(ls) ls_ = LS[0] for i in range(1, 6): ls_ = cv2.pyrUp(ls_) ls_ = cv2.resize(ls_, LS[i].shape[-2::-1]) ls_ = cv2.add(ls_, LS[i]) B = cv2.resize(B, A.shape[-2::-1]) real = np.hstack((A[:, :cols // 2], B[:, cols // 2:])) cv2.imwrite('img/dist/pyramid.jpg', ls_)
# Load our input image image = cv2.imread("./image.jpg") # # Let's make our image 3/4 of it's original size # image_scaled = cv2.resize(image, None, fx=0.75, fy=0.75) # cv2.imshow("Scaling - Linear Interpolation", image_scaled) # cv2.waitKey() # # Let's double the size of our image # img_scaled = cv2.resize(image, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC) # cv2.imshow("Scaling - Cubic Interpolation", img_scaled) # cv2.waitKey() # # Let's skew the re-sizeing by setting exact dimensions # img_scaled = cv2.resize(image, (900, 400), interpolation=cv2.INTER_AREA) # cv2.imshow("Scaling - Skewed Size", img_scaled) # cv2.waitKey() # cv2.destroyAllWindows() smaller = cv2.pyrDown(image) larger = cv2.pyrUp(smaller) cv2.imshow("Original", image) cv2.imshow("Smaller ", smaller) cv2.imshow("Larger", larger) cv2.waitKey() cv2.destroyAllWindows()