# -*- coding: utf-8 -*-
"""
Created on Wed Dec 18 23:43:19 2019
@author: 官宇霞
"""
import os.path
import cv2
import os
import numpy as np
MAX_FEATURES = 10000
GOOD_MATCH_PERCENT = 0.15
def covert_zip():
img_fold_A = 'E:/QQ/test1/before'
img_list1 = os.listdir(img_fold_A)
num_imgs1 = len(img_list1)
for i in range(num_imgs1):
save_fold_A = 'E:/QQ/test1/after'
name_A = img_list1[i]
path_A = os.path.join(img_fold_A, name_A)
im_A = cv2.imread(path_A, 1)
if('.tif' in name_A):
file_name_temp = name_A[:-4]
file_name = os.path.join(save_fold_A , file_name_temp+'.jpg')
cv2.imwrite(file_name, im_A)
image = cv2.imread(file_name)
res = cv2.resize(image, (394,1024), interpolation=cv2.INTER_AREA)
cv2.imwrite(file_name,res)
else:
file_name_temp = name_A[:-4]
file_name = os.path.join(save_fold_A , file_name_temp+'.jpg')
cv2.imwrite(file_name, im_A)
def get_gradient(im) :
# Calculate the x and y gradients using Sobel operator
grad_x = cv2.Sobel(im,cv2.CV_32F,1,0,ksize=3)
grad_y = cv2.Sobel(im,cv2.CV_32F,0,1,ksize=3)
# Combine the two gradients
grad = cv2.addWeighted(np.absolute(grad_x), 0.5, np.absolute(grad_y), 0.5, 0)
return grad
def alignImages(im1,im2):
# Convert images to grayscale
#im1Gray = cv2.cvtColor(im1, cv2.COLOR_BGR2GRAY)
#im2Gray = cv2.cvtColor(im2, cv2.COLOR_BGR2GRAY)
# Detect ORB features and compute descriptors.
orb = cv2.ORB_create(MAX_FEATURES)
keypoints1, descriptors1 = orb.detectAndCompute(im1, None)
keypoints2, descriptors2 = orb.detectAndCompute(im2, None)
#print(descriptors1.shape)
#print(descriptors2.shape)
# Match features.
matcher = cv2.DescriptorMatcher_create(cv2.DESCRIPTOR_MATCHER_BRUTEFORCE_HAMMING)
matches = matcher.match(descriptors1, descriptors2, None)
# Sort matches by score
matches.sort(key=lambda x: x.distance, reverse=False)
# Remove not so good matches
numGoodMatches = int(len(matches) * GOOD_MATCH_PERCENT)
matches = matches[:numGoodMatches]
# Draw top matches
imMatches = cv2.drawMatches(im1, keypoints1, im2, keypoints2, matches, None)
# Extract location of good matches
points1 = np.zeros((len(matches), 2), dtype=np.float32)
points2 = np.zeros((len(matches), 2), dtype=np.float32)
for i, match in enumerate(matches):
points1[i, :] = keypoints1[match.queryIdx].pt
points2[i, :] = keypoints2[match.trainIdx].pt
# Find homography
h, mask = cv2.findHomography(points1, points2, cv2.RANSAC)
# Use homography
height, width = im2.shape
im1Reg = cv2.warpPerspective(im1, h, (width, height))
return im1Reg
def align(cropImg1,cropImg2):
corners1 = cv2.goodFeaturesToTrack(cropImg1,25,0.01,10)
m = int(len(corners1))
corners1 = corners1.reshape(m, 2)
corners2 = cv2.goodFeaturesToTrack(cropImg2,25,0.01,10)
n = int(len(corners2))
corners2 = corners2.reshape(n, 2)
points1 = corners1
points2 = corners2
sumX1 = 0
sumY1 = 0
x,y = points1.shape
for m in range(x):
sumX1 = sumX1 + points1[m][0]
sumY1 = sumY1 + points2[m][1]
sumX2 = 0
sumY2 = 0
x,y = points2.shape
for n in range(x):
sumX2 = sumX2 + points2[n][0]
sumY2 = sumY2 + points2[n][1]
c1 = int((sumX2 / n) - (sumX1 / m))
c2 = int((sumY2 / n) - (sumY1 / m))
cropImg1=np.roll(cropImg1,shift=c1,axis=0)
cropImg1=np.roll(cropImg1,shift=c2,axis=1)
return cropImg1
def clear(img):
img_gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)
gradX = cv2.Sobel(img_gray, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1)
gradY = cv2.Sobel(img_gray, ddepth=cv2.CV_32F, dx=0, dy=1, ksize=-1)
img_gradient = cv2.subtract(gradX, gradY)
img_gradient = cv2.convertScaleAbs(img_gradient)
blurred = cv2.blur(img_gradient, (9, 9))
(_, thresh) = cv2.threshold(blurred, 90, 255, cv2.THRESH_BINARY)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 25))
closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
closed = cv2.erode(closed, None, iterations=4)
closed = cv2.dilate(closed, None, iterations=4)
closed = np.clip(closed, 0, 255)
closed = np.array(closed,np.uint8)
(cnts, _) = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
c = sorted(cnts, key=cv2.contourArea, reverse=True)[0]
rect = cv2.minAreaRect(c)
box = np.int0(cv2.boxPoints(rect))
cv2.drawContours(img, [box], -1, (0, 255, 0), 3)
height,width,length = img.shape
Xs = [i[0] for i in box]
Ys = [i[1] for i in box]
x1 = min(Xs)
x2 = max(Xs)
x1_ = (x2-x1)/2-140
x2_ = (x2-x1)/2+140
if x1_< 0:
x1_ = 0
if x2_ > width:
x2_ = width
y1 = min(Ys)
y2 = max(Ys)
y1_ = (y2 - y1)/2-140
y2_ = (y2 - y1)/2+140
if y1_ < 0:
y1_ = 0
if y2_ > height:
y2_ = height
img2 = img[int(y1_):int(y2_), int(x1_):int(x2_),:]
return img2
if __name__ == '__main__':
covert_zip()
# Read 8-bit color image.
# This is an image in which the three channels are
# concatenated vertically.
for root, dirs, files in os.walk('E:/QQ/test1/after/'): #这里路径需要修改!
for file in files:
print(file)
#讀入圖像
img_path = 'E:/QQ/test1/after/'+file #这里路径需要修改!
im = cv2.imread(img_path,cv2.IMREAD_GRAYSCALE)
# Find the width and height of the color image
sz=im.shape
height = int(sz[0] / 3);
width = sz[1]
print(sz)
# Extract the three channels from the gray scale image
# and merge the three channels into one color image
im_color = np.zeros((height,width,3), dtype=np.uint8 )
for i in range(0,3) :
im_color[:,:,i] = im[ i * height:(i+1) * height,:]
# Allocate space for aligned image
# The blue and green channels will be aligned to the red channel.
# So copy the red channel
# Define motion model
warp_mode = cv2.MOTION_HOMOGRAPHY
# Set the warp matrix to identity.
if warp_mode == cv2.MOTION_HOMOGRAPHY :
warp_matrix = np.eye(3, 3, dtype=np.float32)
else :
warp_matrix = np.eye(2, 3, dtype=np.float32)
# Set the stopping criteria for the algorithm.
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 5000, 1e-10)
# Warp the blue and green channels to the red channel
for i in range(0,2) :
im_color[:,:,i]=alignImages(im_color[:,:,i],im_color[:,:,2])
# Show final output
if '.jpg' in file:
im_color=clear(im_color)
cv2.imwrite('E:/QQ/test1/result/a/'+file,im_color) #这里路径需要修改!
没有合适的资源?快使用搜索试试~ 我知道了~
colorful.zip
共9个文件
ini:4个
py:4个
pyc:1个
需积分: 10 6 下载量 19 浏览量
2020-01-02
13:25:18
上传
评论
收藏 8KB ZIP 举报
温馨提示
数字图像处理,将三通道图合成彩色图像,具体见https://blog.csdn.net/gyx1549624673/article/details/103802574博客讲解。本压缩包中colorful为代码包,其中gray2color.py和covert_zip.py分别是方案一和方案二代码
资源推荐
资源详情
资源评论
收起资源包目录
colorful.zip (9个子文件)
colorful
zip.py 515B
convert.py 546B
gray2color.py 7KB
__pycache__
covert_zip.cpython-35.pyc 906B
covert_zip.py 6KB
.spyproject
codestyle.ini 62B
encoding.ini 64B
vcs.ini 92B
workspace.ini 328B
共 9 条
- 1
资源评论
GUANYX~
- 粉丝: 573
- 资源: 20
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 多机调度问题贪心算法:理论探索与实践应用.zip
- 探索tecreate:软件开发的未来之星.zip
- 打标机项目C#源码连接扫码
- 基于SSM的房屋租赁系统的设计与实现
- xyctf:从入门到精通的实用指南.zip
- mmqrcode1714153659780.png
- Screenshot_2024-04-27-06-08-58-486_com.baidu.xin.aiqicha.jpg
- 基于Javaweb+Tomcat+MySQL的大学生公寓管理系统+sql文件.zip
- 实训作业基于javaweb的订单管理系统源码+数据库+实训报告.zip
- 多机调度问题贪心算法基于最小堆和贪心算法求解多机调度问题.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功