def illum(img): img_gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) _, thresh = cv2.threshold(img_gray, 180, 255, 0) cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) """ # opencv 3.4: ret: img, contours, hierarchy # opencv 4.0 ret: contours, hierarchy """ img_zero = np.zeros(img.shape, dtype=np.uint8) for cnt in cnts: x, y, w, h = cv2.boundingRect(cnt) # x,y是矩阵左上点的坐标,w,h是矩阵的宽和高 img_zero[y:y + h, x:x + w] = 255 mask = img_zero result = cv2.illuminationChange(img, mask, alpha=1, beta=2) for i in [img, mask, result]: cv2.imshow("", i) cv2.waitKey() return result
def force_poisson_blending(source, target, coords): xmin = int(coords[0]) ymin = int(coords[1]) xmax = int(coords[2]) ymax = int(coords[3]) img2 = target[ymin:ymax, xmin:xmax] img1 = cv2.resize(source, (img2.shape[1], img2.shape[0])) flatten_mask = np.zeros(target.shape, dtype=target.dtype) flatten_mask[ymin:ymax, xmin:xmax] = 255 mask = 255 * np.ones(img1.shape, img1.dtype) center = (int((xmin + xmax) / 2), int((ymin + ymax) / 2)) textureflatten = cv2.illuminationChange(target, flatten_mask, alpha=2, beta=2) # cv2.imshow("textureflatten", textureflatten) # cv2.waitKey(0) # cv2.destroyWindow("textureflatten") normal_clone = cv2.seamlessClone(img1, textureflatten, mask, center, cv2.NORMAL_CLONE) # cv2.imshow("normal_clone", normal_clone) # cv2.waitKey(0) # cv2.destroyWindow("normal_clone") mixed_clone = cv2.seamlessClone(img1, textureflatten, mask, center, cv2.MIXED_CLONE) # cv2.imshow("mixed_clone", mixed_clone) # cv2.waitKey(0) # cv2.destroyWindow("mixed_clone") return normal_clone, mixed_clone
def sift_posson_blending(source, target, coords): xmin = int(coords[0]) ymin = int(coords[1]) xmax = int(coords[2]) ymax = int(coords[3]) img2 = target[ymin:ymax, xmin:xmax] img1 = cv2.resize(source, (img2.shape[1], img2.shape[0])) source_kp_image, source_kp, source_des = sift_kp(img1) target_kp_image, target_kp, target_des = sift_kp(img2) matches = get_match(source_des, target_des) print("find %d matches\n" % len(matches)) if len(matches) >= 0: ptsS = np.float32([source_kp[j.queryIdx].pt for j in matches]).reshape(-1, 1, 2) ptsT = np.float32([target_kp[j.trainIdx].pt for j in matches]).reshape(-1, 1, 2) H, status = cv2.findHomography(ptsT, ptsS, cv2.RANSAC, ransacReprojThreshold) imgOut = cv2.warpPerspective(img1, H, (img2.shape[1], img2.shape[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP) cv2.imshow("result", imgOut) cv2.waitKey(0) cv2.destroyWindow("result") flatten_mask = np.zeros(target.shape, dtype=target.dtype) flatten_mask[ymin:ymax, xmin:xmax] = 255 mask = 255 * np.ones(imgOut.shape, imgOut.dtype) center = (int((xmin + xmax) / 2), int((ymin + ymax) / 2)) textureflatten = cv2.illuminationChange(target, flatten_mask, alpha=2, beta=2) cv2.imshow("textureflatten", textureflatten) cv2.waitKey(0) cv2.destroyWindow("textureflatten") normal_clone = cv2.seamlessClone(imgOut, textureflatten, mask, center, cv2.NORMAL_CLONE) cv2.imshow("normal_clone", normal_clone) cv2.waitKey(0) cv2.destroyWindow("normal_clone") mixed_clone = cv2.seamlessClone(imgOut, target, mask, center, cv2.MIXED_CLONE) cv2.imshow("mixed_clone", mixed_clone) cv2.waitKey(0) # cv2.destroyWindow("mixed_clone") return normal_clone, mixed_clone
def illum(img): # img = cv2.imread("test2.jpg") # img = img[532:768, 0:512] img_bw = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) thresh = cv2.threshold(img_bw, 180, 255, 0)[1] cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1] img_zero = np.zeros(img.shape, dtype=np.uint8) # img[thresh == 255] = 150 for cnt in cnts: x, y, w, h = cv2.boundingRect(cnt) img_zero[y:y + h, x:x + w] = 255 # cv2.imshow("mask", mask) mask = img_zero # cv2.imshow("mask", mask) result = cv2.illuminationChange(img, mask, alpha=1, beta=2) # cv2.imshow("result", result) # cv2.waitKey(0) return result
img_reverse = img_reverse[padding[1]:img_reverse.shape[0] - padding[3], padding[0]:img_reverse.shape[1] - padding[2], ::-1] print('reverse shape:', img_reverse.shape, img_reverse.min(), img_reverse.max()) img_trans = np.zeros_like(img, img.dtype) img_trans[y0:y1, x0:x1, :] = img_reverse img_mix = img * ~mask[..., np.newaxis] + img_trans * mask[..., np.newaxis] mask_neigh = dilation(forehead_line(mask, kpt), square( (x1 - x0) // 15)) print('mask neigh shape:', mask_neigh.shape) #cv2.imwrite('tmp_forehead.jpg', mask_neigh.astype(np.uint8)*255) img_mix_smooth = (gaussian(img_mix, sigma=0.6, multichannel=False) * 255).astype(img_mix.dtype) #cv2.imwrite('tmp_smooth.jpg', img_mix_smooth) img_mix = img_mix_smooth * mask_neigh[..., np.newaxis] + img_mix * ( ~mask_neigh)[..., np.newaxis] #cv2.imwrite('tmp_forehead.jpg', mask_neigh) cv2.imwrite( os.path.join(opt.results_dir, file.rsplit('/', 1)[-1].replace('_ori', '')), cv2.illuminationChange(img_mix, mask_neigh.astype(img_mix.dtype) * 255)) shutil.copy( file, os.path.join(opt.results_dir, file.rsplit('/', 1)[-1].replace('.jpg', '_ori.jpg')))
if (colmax > gray.shape[1]): colmax = gray.shape[1] imageROI = gray[rowmin:rowmax, colmin:colmax] temaver = np.mean(imageROI) blockImage[r, c] = temaver blockImage = blockImage - average blockImage2 = cv2.resize(blockImage, (gray.shape[1], gray.shape[0]), interpolation=cv2.INTER_CUBIC) gray2 = gray.astype(np.float32) dst = gray2 - blockImage2 dst = dst.astype(np.uint8) dst = cv2.GaussianBlur(dst, (3, 3), 0) dst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR) return dst if __name__ == '__main__': file = 'test6.jpg' blockSize = 16 img = cv2.imread(file) dst = unevenLightCompensate(img, blockSize) # result = np.concatenate([img, dst], axis=1) cv2.imshow('result', dst) cv2.waitKey(0) cv2.illuminationChange()
def poissonEdit(self, btnNor, btnMix, cheFFla, sliL, sliH, cheIllu, slia, slib, cheFCo, cheGr): if self.sourceMode: if self.dst_img is None or self.src_img is None: return src, mask, _x, _y = self.getEditSrc() if btnNor.isChecked(): cloneMode = cv2.NORMAL_CLONE elif btnMix.isChecked(): cloneMode = cv2.MIXED_CLONE else: cloneMode = cv2.MONOCHROME_TRANSFER try: self.result_img = cv2.seamlessClone(src, self.dst_img, mask, (_x, _y), cloneMode) except Exception as e: print(e) print(roi_u, roi_d, roi_l, roi_r) print(src_u, src_d, src_l, src_r) return self.display_res() return if self.src_img is None: return res = self.src_img.copy() if cheFCo.isChecked(): r = self.setColor.red() g = self.setColor.green() b = self.setColor.blue() if r + g + b != 765: _b, _g, _r = cv2.split( cv2.bitwise_and( res, cv2.merge((self.mask, self.mask, self.mask)))) num = np.sum(self.mask / 255) mulr, mulg, mulb = r * num / np.sum(_r), g * num / np.sum( _g), b * num / np.sum(_b) mulr, mulg, mulb = mulr**0.5, mulg**0.5, mulb**0.5 if mulr < 0.5: mulr = 0.5 if mulr > 2.5: mulr = 2.5 if mulg < 0.5: mulg = 0.5 if mulg > 2.5: mulg = 2.5 if mulb < 0.5: mulb = 0.5 if mulb > 2.5: mulb = 2.5 print(mulr, mulg, mulb) res = cv2.colorChange(res, self.mask, mulr, mulg, mulb) if cheGr.isChecked(): dst = cv2.cvtColor( cv2.cvtColor(self.src_img, cv2.COLOR_BGR2GRAY), cv2.COLOR_GRAY2BGR) src, mask, _x, _y = self.getEditSrc(res, False) res = cv2.seamlessClone(src, dst, mask, (_x, _y), cv2.NORMAL_CLONE) if cheFFla.isChecked(): res = cv2.textureFlattening(res, self.mask, res, sliL.value(), sliH.value()) if cheIllu.isChecked(): res = cv2.illuminationChange(res, self.mask, res, (slia.value() + 1) / 50, (slib.value() + 1) / 100) self.result_img = res self.display_res()