def carveSeams(img, seams, show=False): h, w = img.shape[:2] seam_map = np.tile(np.arange(w), (h, 1)) for i in range(seams): seam = getSeam(img) seam_map = removeSeam(seam_map, seam) img = removeSeam(img, seam) if show: funcs.show(img, False) return img, seam_map
import numpy as np import cv2 import funcs img=cv2.imread("image.png",0) img=np.float32(cv2.GaussianBlur(img,(5,5),0)) derivative_kernel=np.float32([[-1,0,1],[-2,0,2],[-1,0,1]]) Ix=cv2.filter2D(img, -1, derivative_kernel) Iy=cv2.filter2D(img, -1, derivative_kernel.T) I=Ix**2+Iy**2 funcs.show(I**.5) path=I*1 kernel=np.float32([[1,1,1]]) h,w=img.shape[:2] for i in range(1,h): row=path[i-1,:] row=cv2.erode(row,kernel.T) path[i:i+1,:]+=row.T x=np.argmin(path[-1]) y=h-1 seam=[] seam.append(x) while y>0: y-=1 x=x+1 or x or x-1 seam.insert(x,0) print seam funcs.show(path)
src_pts = [] dst_pts = [] for match in matches: src_pts.append(kp1[match.queryIdx].pt) dst_pts.append(kp2[match.trainIdx].pt) src_pts = np.array(src_pts) dst_pts = np.array(dst_pts) M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) img1_transformed = cv2.warpPrespective(img1, M, img1.shape[::-1]) """ img=cv2.imread('image1.png',0) #funcs.show(img) #img=cross_correlate(img,51) #funcs.show(img) img=cv2.GaussianBlur(img,(5,5),0,borderType=cv2.BORDER_REFLECT_101) kernel=np.array([[0,0,0],[1,0,-1],[0,0,0]]) Ix=cv2.filter2D(img,-1,kernel,borderType=cv2.BORDER_REFLECT_101) Iy=cv2.filter2D(img,-1,kernel.T,borderType=cv2.BORDER_REFLECT_101) thing=cv2.cornerHarris(np.uint8(img),2,3,0.04) """ """Dilate makes local maxes stick out""" """ thing_dilate=cv2.dilate(thing,np.ones((15,15))) corners=thing_dilate==thing corners[thing<.1*thing.max()]=0 funcs.show(thing) funcs.show(thing_dilate) funcs.show(corners) xs,ys=corners """
row = img[i] row = np.concatenate((row[:seam[i]], row[seam[i] + 1:]), axis=0) out[i] = row return out def carveSeams(img, seams, show=False): h, w = img.shape[:2] seam_map = np.tile(np.arange(w), (h, 1)) for i in range(seams): seam = getSeam(img) seam_map = removeSeam(seam_map, seam) img = removeSeam(img, seam) if show: funcs.show(img, False) return img, seam_map img1 = cv2.imread('image1_small.png') seam1 = getSeam(img1) print "show seam" img1 = showSeam(img1, seam1) funcs.show(img1) img3 = cv2.imread('image1_small.png') img3 = img3 print "carve seam" img3, _ = carveSeams(img3, 400, True) funcs.show(img3)
f.write(struct.pack("HHB", h, w, d)) for num in flat: f.write(struct.pack("B", num)) f.close() def openImg(filename): f = file(filename + ".swrd", "rb") h, w, d = struct.unpack("HHB", f.read(5)) shape = (h, w, d) nums = [] for i in range(h * w * d): nums.append(struct.unpack("B", f.read(1))) img = np.uint8(nums) img = img.reshape(shape) #img+=16*np.random.rand(*shape) return np.uint8(img) img = cv2.imread("image1.png") #funcs.show(img) save(img, "test2") img2 = openImg("test2") cv2.imwrite("test2.png", img2) funcs.show(img2) #img2=flat.reshape((500,333,3)) #funcs.show(img2)
def test_funcs(img1, img2): img3 = img2 funcs.show(img1) funcs.save(img1, filename='out_1.png') #funcs.getWidth funcs.getHeight print funcs.getWidth(img1), funcs.getHeight(img1) #funcs.mix mixed_pics = funcs.mix(img1, img3, .5) funcs.show(mixed_pics) #funcs.tint tinted_pic = funcs.tint(img1, (0, 0, 255), .5) funcs.show(tinted_pic) #funcs.grayscale gray_pic = funcs.grayscale(img1) funcs.show(gray_pic) gray_pic_2 = funcs.grayscale(img1, two_dimensional=True) funcs.show(gray_pic_2) #funcs.rotate img_rotate = funcs.rotate(img1, 180) funcs.show(img_rotate)