资源简介
基于python+opencv的目标图像自动识别提取感兴趣区域(本项目提取矩形目标区域)
代码片段和文件信息
#coding:utf-8
import cv2
import numpy as np
import math
srcWidth = 0
srcHeight = 0
maxWidth = 1024
maxHeight = 600
class Config:
def __init__(self):
pass
src = “IMG_9571.jpg“
min_area = 100000
min_contours = 8
threshold_thresh = 50
epsilon_start = 50
epsilon_step = 10
class HoughPoints:
def __init__(selfrho=0theta=0):
self.rhos = [rho]
self.thetas = [theta]
‘‘‘
@func 根据HoughLines转换出直线点
@param rho 距离
@param theta 角度
‘‘‘
def rt_to_point(img rho theta):
#垂直直线
if (theta < (np.pi/4. )) or (theta > (3.*np.pi/4.0)):
#该直线与第一行的交点
pt1 = (int(rho/np.cos(theta))0)
#该直线与最后一行的焦点
pt2 = (int((rho-img.shape[0]*np.sin(theta))/np.cos(theta))img.shape[0])
return pt1 pt2
else:
#水平直线 该直线与第一列的交点
pt1 = (0int(rho/np.sin(theta)))
#该直线与最后一列的交点
pt2 = (img.shape[1] int((rho-img.shape[1]*np.cos(theta))/np.sin(theta)))
return pt1 pt2
‘‘‘
@return [top-left top-right bottom-right bottom-left]
‘‘‘
def order_points(pts):
# initialzie a list of coordinates that will be ordered
# such that the first entry in the list is the top-left
# the second entry is the top-right the third is the
# bottom-right and the fourth is the bottom-left
rect = np.zeros((4 2) dtype=“float32“)
# the top-left point will have the smallest sum whereas
# the bottom-right point will have the largest sum
s = pts.sum(axis=1)
rect[0] = pts[np.argmin(s)]
rect[2] = pts[np.argmax(s)]
# now compute the difference between the points the
# top-right point will have the smallest difference
# whereas the bottom-left will have the largest difference
diff = np.diff(pts axis=1)
rect[1] = pts[np.argmin(diff)]
rect[3] = pts[np.argmax(diff)]
# return the ordered coordinates
return rect
def point_distance(ab):
return int(np.sqrt(np.sum(np.square(a - b))))
‘‘‘
@func 计算两条直线的交点
‘‘‘
def line_intersection(line1 line2):
xdiff = (line1[0][0] - line1[1][0] line2[0][0] - line2[1][0])
ydiff = (line1[0][1] - line1[1][1] line2[0][1] - line2[1][1])
def det(a b):
return a[0] * b[1] - a[1] * b[0]
div = det(xdiff ydiff)
if div == 0:
raise Exception(‘lines do not intersect‘)
d = (det(*line1) det(*line2))
x = det(d xdiff) / div
y = det(d ydiff) / div
return x y
‘‘‘
@func 迪卡尔转极坐标
‘‘‘
def cart_to_polor(x1 y1 x2 y2):
diff = float(abs(x1-x2)) / abs(y1-y2)
theta = math.atan(diff)
#print(“theta=%f diff=%f %f %f“%(theta diff abs(x1-x2) abs(y1-y2)))
rho = math.sin(theta)*(y1 - (x1/math.tan(theta)) )
return rho theta
image = cv2.imread(Config.src)
gray = cv2.cvtColor(image cv2.COLOR_BGR2GRAY)
srcWidth srcHeight channels = image.shape
print(srcWidth srcHeight)
# 中值平滑,
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 6929 2018-08-12 13:53 Opencv_python\Opencv2_python.py
文件 836761 2018-08-12 13:46 Opencv_python\图像处理要求_20180730更新.pdf
文件 1649843 2018-08-11 21:12 Opencv_python\未通过图片\IMG_9380.JPG
文件 2241514 2018-08-11 21:14 Opencv_python\未通过图片\IMG_9382.JPG
文件 2165282 2018-08-11 21:14 Opencv_python\未通过图片\IMG_9385.JPG
文件 1865957 2018-08-11 21:13 Opencv_python\未通过图片\IMG_9397.JPG
文件 1717046 2018-08-11 21:12 Opencv_python\测试通过的图片\5.JPG
文件 1664460 2018-08-11 21:12 Opencv_python\测试通过的图片\IMG_9378.JPG
文件 1697926 2018-08-11 21:12 Opencv_python\测试通过的图片\IMG_9379.JPG
文件 1682140 2018-08-11 21:12 Opencv_python\测试通过的图片\IMG_9399.JPG
文件 1710356 2018-08-11 21:12 Opencv_python\测试通过的图片\IMG_9429.JPG
文件 1714546 2018-08-11 21:12 Opencv_python\测试通过的图片\IMG_9432.JPG
文件 1642283 2018-08-11 21:12 Opencv_python\测试通过的图片\IMG_9433.JPG
文件 1678062 2018-08-11 21:12 Opencv_python\测试通过的图片\IMG_9435.JPG
文件 1717046 2018-08-11 21:12 Opencv_python\测试通过的图片\IMG_9436.JPG
文件 1840351 2018-08-11 21:13 Opencv_python\测试通过的图片\IMG_9437.JPG
文件 1768708 2018-08-11 21:13 Opencv_python\测试通过的图片\IMG_9438.JPG
文件 1908407 2018-08-11 21:14 Opencv_python\测试通过的图片\IMG_9439.JPG
文件 1793371 2018-08-11 21:13 Opencv_python\测试通过的图片\IMG_9440.JPG
文件 2176392 2018-08-11 21:14 Opencv_python\测试通过的图片\IMG_9468.JPG
文件 2183829 2018-08-11 21:14 Opencv_python\测试通过的图片\IMG_9469.JPG
文件 2144229 2018-08-11 21:14 Opencv_python\测试通过的图片\IMG_9471.JPG
文件 3957927 2018-08-12 13:35 Opencv_python\算法测试矫正图片\transfer115.png
文件 2019301 2018-08-12 13:40 Opencv_python\算法测试矫正图片\transfer26.png
文件 3569918 2018-08-12 12:43 Opencv_python\算法测试矫正图片\transfer3.png
文件 2260877 2018-08-12 12:49 Opencv_python\算法测试矫正图片\transfer338.png
文件 3179354 2018-08-12 13:21 Opencv_python\算法测试矫正图片\transfer35.png
文件 2625796 2018-08-12 12:40 Opencv_python\算法测试矫正图片\transfer36.png
文件 3081279 2018-08-12 12:55 Opencv_python\算法测试矫正图片\transfer373.png
文件 4195687 2018-08-12 13:17 Opencv_python\算法测试矫正图片\transfer3909.png
............此处省略16个文件信息
相关资源
- python实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- scrapy_qunar_one
- Python学习全系列教程永久可用
- python简明教程.chm
- 抽奖大转盘python的图形化界面
- 双边滤波器实验报告及代码python
- python +MYSQL+HTML实现21蛋糕网上商城
- Python-直播答题助手自动检测出题搜索
- OpenCV入门教程+OpenCV官方教程中文版
- Python 串口工具源码+.exe文件
- Python开发的全栈股票系统.zip
- Python操作Excel表格并将其中部分数据写
- python书籍 PDF
- 利用python绘制散点图
- python+labview+No1.vi
- 老男孩python项目实战
- python源码制作whl文件.rar
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- 计算机科学导论-python.pdf
- python模拟鼠标点击屏幕
- windows鼠标自动点击py脚本
- 鱼c小甲鱼零基础学python全套课后题和
- Python 练习题100道
- Practical Programming 2nd Edition
- wxPython Application Development Cookbook
- python 3.6
- Python 3.5.2 中文文档 互联网唯一CHM版本
- python3.5.2.chm官方文档
评论
共有 条评论