
通过opencv,识别出两张不同部分。俗称大家一起来找茬
效果图
源代码 image_detect_03py
import cv2import numpy as np
from matplotlib import pyplot as plt
import argparse
def matchAB(fileA, fileB):
# 读取图像数据
imgA = cv2imread(fileA)
imgB = cv2imread(fileB)
# 转换成灰色
grayA = cv2cvtColor(imgA, cv2COLOR_BGR2GRAY)
grayB = cv2cvtColor(imgB, cv2COLOR_BGR2GRAY)
# 获取A的大小
height, width = grayAshape
# 取局部图像,寻找匹配位置
result_window = npzeros((height, width), dtype=imgAdtype)
for start_y in range(0, height-100, 10):
for start_x in range(0, width-100, 10):
window = grayA[start_y:start_y+100, start_x:start_x+100]
match = cv2matchTemplate(grayB, window, cv2TM_CCOEFF_NORMED)
_, _, _, max_loc = cv2minMaxLoc(match)
matched_window = grayB[max_loc[1]:max_loc[1]+100, max_loc[0]:max_loc[0]+100]
result = cv2absdiff(window, matched_window)
result_window[start_y:start_y+100, start_x:start_x+100] = result
# 用四边形圈出不同部分
_, result_window_bin = cv2threshold(result_window, 30, 255, cv2THRESH_BINARY)
_, contours, _ = cv2findContours(result_window_bin, cv2RETR_EXTERNAL, cv2CHAIN_APPROX_SIMPLE)
imgC = imgAcopy()
for contour in contours:
min = npnanmin(contour, 0)
max = npnanmax(contour, 0)
loc1 = (min[0][0], min[0][1])
loc2 = (max[0][0], max[0][1])
cv2rectangle(imgC, loc1, loc2, 255, 2)
pltsubplot(1, 3, 1), pltimshow(cv2cvtColor(imgA, cv2COLOR_BGR2RGB)), plttitle('A'), pltxticks([]), pltyticks([])
pltsubplot(1, 3, 2), pltimshow(cv2cvtColor(imgB, cv2COLOR_BGR2RGB)), plttitle('B'), pltxticks([]), pltyticks([])
pltsubplot(1, 3, 3), pltimshow(cv2cvtColor(imgC, cv2COLOR_BGR2RGB)), plttitle('Answer'), pltxticks([]), pltyticks([])
pltshow()
if __name__ == '__main__':
parser = argparseArgumentParser()
parseradd_argument(
'--source_image',
type=str,
default='img/image01-0png',
help='source image'
)
parseradd_argument(
'--target_image',
type=str,
default='img/image01-1png',
help='target image'
)
FLAGS, unparsed = parserparse_known_args()
matchAB(FLAGSsource_image, FLAGStarget_image)
用image模块更直接,可以用getpixel获得像素值,给你个例子吧。
01#!/usr/bin/env python
02import Image
03import sys
04im = Imageopen(sysargv[1])
05width = imsize[0]
06height = imsize[1]
07print "/ width:%d /"%(width)
08print "/ height:%d /"%(height)
09count = 0
10for h in range(0, height):
11 for w in range(0, width):
12 pixel = imgetpixel((w, h))
13 for i in range(0,3):
14 count = (count+1)%16
15 if (count == 0):
16 print "0x%02x,/n"%(pixel[i]),
17 else:
18 print "0x%02x,"%(pixel[i]),
先对进行二值化处理。
由于这些格子大小均匀,所以可以使用常规的除法,先裁剪出第一个格子:
e=a[:,:,0]shape
f=a[0:int(e[0]/9),0:int(e[1]/9)]
ioimsave('00png',f)
下图是等比例放大的效果图。
第一列第二个格子:
m=1
n=0
f=a[int(me[0]/9):int((m+1)e[0]/9),int(ne[1]/9):int((n+1)e[1]/9)]
第一行第二个格子:
m=0
n=1
f=a[int(me[0]/9):int((m+1)e[0]/9),int(ne[1]/9):int((n+1)e[1]/9)]
这样,用for循环,可以实现全部裁剪:
for m in range(9):
for n in range(9):
f=a[int(me[0]/9):int((m+1)e[0]/9),int(ne[1]/9):int((n+1)e[1]/9)]
ioimsave('0/'+str(m)+'-'+str(n)+'png',f)
去除边界的黑框,只保留数字或空白:
for m in range(9):
for n in range(9):
f=a[int(me[0]/9)+10:int((m+1)e[0]/9)-10,int(ne[1]/9)+10:int((n+1)e[1]/9)-10]
ioimsave('0/'+str(m)+'-'+str(n)+'png',f)
以上就是关于python3 image_detect_03.py --source_image=***.png --target_image=***.png全部的内容,包括:python3 image_detect_03.py --source_image=***.png --target_image=***.png、使用python PIL处理图片。怎么获取图片的像素数据、在python中怎么裁剪图片大小如何利用Python裁剪图片等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)