ios9分屏两边有人试了吗

learning opencv 3(python)学习笔记(2)_美文阅读,精品文摘网
当前位置: &>&&>& >
learning opencv 3(python)学习笔记(2)
这一章基本都是对于图像的一些基本处理
这里介绍了高通滤波器HPF,根据像素和周围像素的亮度差值来提升(boost)该像素的亮度的滤波器(这句话是书上翻译过来的原话)
kernel_3x3 = np.array([[-1, -1, -1],
[-1, 8, -1],
[-1, -1, -1]])
kernel_5x5 = np.array([[-1, -1, -1, -1, -1],
[-1, 1, 2, 1, -1],
[-1, 2, 4, 2, -1],
[-1, 1, 2, 1, -1],
[-1, -1, -1, -1, -1]])
img = cv2.imread("test_pic.jpg", 0)
k3 = ndimage.convolve(img, kernel_3x3)
#ndimage是scipy的模块,这句就是让img和核做卷积运算,高通滤波
k5 = ndimage.convolve(img, kernel_5x5)
blurred = cv2.GaussianBlur(img, (11, 11), 0)
#这个是另一种手段,用cv2自带的低通滤波(一个高斯模糊的低通,第二个参数不明),然后在下面做差,来做成高通滤波(效果要比前两个好)
g_hpf = img - blurred
这里顺便介绍了低通滤波器LPF, 在像素与周围像素的亮度差小于一个特定值时,平滑该像素的亮度。用于去噪和模糊化
要么这本书是辣鸡,要么这个翻译把代码全删了,谁来救救我。。。。这里缺失了大量代码,不过还好,github是万能的我是万能的github
最后给出来一个边缘描绘和肖像胶卷色彩的代码的例子,且,那些底层的class都没有给出,给出的他都没用。。。我也是醉了,顺便,那个肖像胶卷色彩,没看出来效果。。
在上一章的cameo.py里面做了如下修改
import filters
self._captureManager = CaptureManager(
cv2.VideoCapture(0), self._windowManager, True)
+self._crveFilter= filters.BGRPortraCurveFilter()
#没啥东西,创建个BGRPortraCurveFilter
+filters.strokeEdges(frame, frame)
+self._crveFilter.apply(frame, frame)
self._captureManager.exitFrame()
这里是后面
def strokeEdges(src, dst, blurKsize = 7, edgeKsize = 5):
#blurKsize作为medianBlur函数的ksize参数
#edgeKsize作为laplacian函数的ksize参数
#即,一个中值模糊,medianBlur加一个拉普拉斯边缘检测,laplacian
if blurKsize &= 3: #blurKsize&=3才能做中值模糊,我也不知道为啥。。
blurredSrc = cv2.medianBlur(src, blurKsize)
#将图片转换为灰度图片
graySrc = cv2.cvtColor(blurredSrc, cv2.COLOR_BGR2GRAY)
graySrc = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
cv2.Laplacian(graySrc, cv2.CV_8U, graySrc, ksize = edgeKsize)
#拉普拉斯的边缘检测,参数懵逼
normalizedInverseAlpha = (1.0 / 255) * (255 - graySrc)
#归一化操作,使像素值在0-1之间
channels = cv2.split(src)
#将src的通道split后保存
for channel in channels:
#遍历每个通道,给每个通道都*计算的系数
channel[:] = channel * normalizedInverseAlpha
cv2.merge(channels, dst)
#将颜色通道合并回去
这里是被懵逼的关键,因为这句话。。。没啥效果啊。。。
def createCurveFunc(points):
"""Return a function derived from control points."""
if points is None:
return None
numPoints = len(points)
#获取点集长度
if numPoints & 2:
return None
#点少于两个不干
xs, ys = zip(*points)
#zip,可以将[1, 2, 3], [4, 5, 6]组成一个二元组:
#[(1, 4), (2, 5), (3, 6)]
if numPoints & 4:
#如果点少于4个,直接用线性模型
kind = 'linear'
# 'quadratic' is not implemented.
#二次线性方程没写。。。是这个意思么。。。
kind = 'cubic'
#不然就用cubic,立方模型。。。什么鬼
return scipy.interpolate.interp1d(xs, ys, kind,
bounds_error = False)
#然后就调用了一个屌屌的函数,scipy.interpolate.interp1d
#就是做曲线拟合的,然后会返回给你一个函数,由办法知道函数内容不。。。
def createLookupArray(func, length = 256):
"""Return a lookup for whole-number inputs to a function.
The lookup values are clamped to [0, length - 1].
if func is None:
return None
lookupArray = numpy.empty(length)
#lookupArray清零
while i & length:
#遍历length长度
func_i = func(i)
#根据x算y恩。。。这里的x就是从0开始的整数。。
lookupArray[i] = min(max(0, func_i), length - 1)
#lookupArray为
#在函数计算后,所有大于0且小于数据长度的值,这样估计是为了保证不过界,不过究竟是图啥
return lookupArray
def createCompositeFunc(func0, func1):
"""Return a composite of two functions."""
if func0 is None:
return func1
if func1 is None:
return func0
return lambda x: func0(func1(x))
#lambda,用于一个只用一次的函数
#这里的意思就是返回func0(func1(x))这个函数。法克啊。
filters.py
class BGRFuncFilter(object):
#虽然读完了这个函数,但是并不知道是图啥的,图像也没啥效果。。。
"""A filter that applies different functions to each of BGR."""
def __init__(self, vFunc = None, bFunc = None, gFunc = None,
rFunc = None, dtype = numpy.uint8):
length = numpy.iinfo(dtype).max + 1
#iinfo能够获取一些类型的属性,这里获取了dtype这种不一定是啥类型的最大值,然后+1
self._bLookupArray = utils.createLookupArray(
utils.createCompositeFunc(bFunc, vFunc), length)
#嗯,utils.createLookupArray继续向上看,还有utils.createCompositeFunc,这里的第第一个参数为bFunc(vFunc(x))这个函数
self._gLookupArray = utils.createLookupArray(
utils.createCompositeFunc(gFunc, vFunc), length)
#对应到bgr上了,所以v并不知道是干啥的
self._rLookupArray = utils.createLookupArray(
utils.createCompositeFunc(rFunc, vFunc), length)
def apply(self, src, dst):
"""Apply the filter with a BGR source/destination."""
b, g, r = cv2.split(src) #切分
utils.applyLookupArray(self._bLookupArray, b, b)
#嗯,迷之utils.applyLookupArray
utils.applyLookupArray(self._gLookupArray, g, g)
utils.applyLookupArray(self._rLookupArray, r, r)
cv2.merge([b, g, r], dst) #合并
class BGRCurveFilter(BGRFuncFilter):
"""A filter that applies different curves to each of BGR."""
def __init__(self, vPoints = None, bPoints = None,
gPoints = None, rPoints = None, dtype = numpy.uint8):
#反正就是继续往下丢。。。
BGRFuncFilter.__init__(self,
utils.createCurveFunc(vPoints),
#这里将点集做成了函数,utils.createCurveFunc,其实是在做线性拟合
utils.createCurveFunc(bPoints),
utils.createCurveFunc(gPoints),
utils.createCurveFunc(rPoints), dtype)
class BGRPortraCurveFilter(BGRCurveFilter):
"""A filter that applies Portra-like curves to BGR."""
def __init__(self, dtype = numpy.uint8):
#一堆初始化
BGRCurveFilter.__init__(
vPoints = [(0,0),(23,20),(157,173),(255,255)],
bPoints = [(0,0),(41,46),(231,228),(255,255)],
gPoints = [(0,0),(52,47),(189,196),(255,255)],
rPoints = [(0,0),(69,69),(213,218),(255,255)],
dtype = dtype)
不知道出于什么考虑,canny的介绍非常的简单,嗯。。。法克
img = cv2.imread("./test_pic.jpg", 0)
cv2.imwrite("canny.jpg", cv2.Canny(img, 0, 100))
#嗯,只需要调用imwrite+Canny就可以了,顺便,那两个参数也没介绍,顺便,就说明了一下canny算法很复杂,有5个步骤,由于那些步骤我看不懂,所以先过吧。。。阿西吧
这里是轮廓检测循序渐进第一站
img = np.zeros((200, 200), dtype=np.uint8)
#创建一个200x200的空白图像
img[50:150, 50:150] = 255 #然后把其中的一块50,50-150,150搞成白色
ret, thresh = cv2.threshold(img, 127, 255, 0)
#二值化操作,返回的thresh是二值化的结果
image, contours, hierarchy = cv2.findContours(thresh,
cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#然后做轮廓查找操作
#三个参数:输入图像,层次类型,轮廓逼近方法
#这个函数会修改输入图像,所以最好用copy
#cv2.RETR_TREE会得到整体的轮廓层次,如果只要最外面的应该实用RETR_EXTERNAL
#三个返回值:修改后的图像,图像的轮廓,它们的层次
color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
#获取img的的BGR色彩
img = cv2.drawContours(color, contours, -1, (0, 255, 0), 2)
#轮廓绘画,第四个参数,把轮廓画成绿色
轮廓检测进阶
img = cv2.pyrDown(cv2.imread("./test_pic.jpg", cv2.IMREAD_UNCHANGED))
#pyrDown平滑图片,使用高斯金字塔结构向下采样,
#IMREAD_UNCHANGED使用alpha4通道色彩
ret, thresh = cv2.threshold(cv2.cvtColor(img.copy(),
cv2.COLOR_BGR2GRAY), 127, 255, cv2.THRESH_BINARY)
image, contours, hier = cv2.findContours(thresh,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#找到外轮廓,image修改后的图像,contours图像的轮廓,hier他们的层次,关于contours和hier,打印了一下,并没理解保存的方式。。。
for c in contours:
#遍历每一个contours,我要认为他们是像素点么。。
# find bounding box cordinates
x, y, w, h = cv2.boundingRect(c)
#从获得的东西来看,是一系列的矩形
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
#在img上面画矩形,绿色,2忘了是啥
# find minimum area
rect = cv2.minAreaRect(c)
#计算包围目标的最小矩形区域
# calculate coordinates of the minimum area rectangle
box = cv2.boxPoints(rect)
#计算四个顶点的坐标
# normalize coordinates to integers
box = np.int0(box)
#需要先转换成int型
# draw contours
cv2.drawContours(img, [box], 0, (0, 0, 255), 3)
#然后再给他们都画出来
# calculate center and radius of minimum enclosing circle
(x, y),radius = cv2.minEnclosingCircle(c)
#轮廓们的最小闭圆
# cast to integers
center = (int(x), int(y))
radius = int(radius)
#送他们回int
# draw the circle
img = cv2.circle(img, center, radius, (0, 255, 0), 2)
epsilon = 0.01 * cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, epsilon, True)
#approxPloyDP计算近似多边形框,第一个参数是轮廓,第二个参数是“不会念值”表示源轮廓与近似多边形的最大差值,值越小与原多边形越接近
#第三个参数是布尔标记,这个多边形是否闭合
#这里第二个参数是用arcLength,多边形周长,计算出来的
hull = cv2.convexHull(c)
#convexHull能得到轮廓凸多边形信息
cv2.drawContours(img, [approx], 0, (0, 0, 255), 1)
cv2.drawContours(img, [hull], 0, (0, 255, 0), 1)
cv2.drawContours(img, contours, -1, (255, 0, 0), 1)
#画出所有轮廓
img = cv2.pyrDown(cv2.imread('./test_pic.jpg'))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 120)
#用canny检测边沿
minLineLength = 100
maxLineGap = 5
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength, maxLineGap)
#在边沿基础上找直线
#第一个是图,第二第三是集合表示rho,theta使用的是常用值
#第三个是阈值,达到阈值的会保留
#第四个是最小线段长度
#第五个是一条线段的间隙长度,小于这个长度会被当成两个线段
for x in range(0, len(lines)):
for x1,y1,x2,y2 in lines[x]:
print(x1, y1, x2, y2)
cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 1)
#遍历,画直线
planets = cv2.pyrDown(cv2.imread('./test_pic.jpg'))
gray_img = cv2.cvtColor(planets, cv2.COLOR_BGR2GRAY)
#变灰度图像
img = cv2.medianBlur(gray_img, 5)
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
#不知道干啥的,啥也没干
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 120,
param1=100, param2=30, minRadius=0, maxRadius=0)
#圆的检测,参数跟直线的差不多(犯懒中)
circles = np.uint16(np.around(circles))
#都给int了
for i in circles[0, :]:
# draw the outer circle
cv2.circle(planets, (i[0], i[1]), i[2], (0, 255, 0), 2)
# draw the center of the circle
cv2.circle(planets, (i[0], i[1]), 2, (0, 0, 255), 3)
#画圆心就是一个半径为2的圆
延伸阅读:
(责任编辑:美朵)
每日重点推荐
夏医生是自己多年老同事的儿子,国外学成归来做事业,老同事担心儿子诊所刚开,门厅罗雀伤了自尊,便偷偷塞钱给李梅,求她装病去“捧场”。
04-05 12-15 12-15
一周热点文章
在线阅读专题当前位置: >>
解决OPENCV读取PNG的alpha通道
解决 OPENCV 读取 PNG 的 alpha 通道,并且进行图片叠加的问题( 14:38:22)转 载opencv 在 2.3(包括)之前是无法读取 PNG 的 alpha 通道的,这就导致如果要给当前视频打 水印,即使抠图很完美,都会留下黑边或者白边。原因就在于图像与图
像叠加的时候,是通 过 alpha 进行混合的。 这一点用比较高端的说法, 就是抗锯齿。 位图的边缘像素只有通过 alpha 和底层位图进行混合,才能再视觉上产生平滑过渡的效果,否则就会产生颗粒。在 OpenGL 等三维渲染中还会采用多层 mipmap 纹理贴图或者 HDR 技术来增加物体变化时候的平滑效 果,这些就不讲了。 使用 opencv 版本 2.4 以上,调用 cv::Mat img = imread(&pic.png&, -1)就可以读取 4 通道的 png 图像。其中第四个通道的数据类型和其他通道的一样,都是 uchar 型,完全透明为 0,否则 为 255。 注意如果使用 imshow 方法,依然还是无法显示 4 通道的图像的,这点 opencv 到现在都不 改实在是蛋疼。 所以要确认自己版本的 imread 确实可以读取 4 通道的话, 可以直接用 imwirte 输出 png 图像来测试。 叠加到原图的方法就不多说了, 就是取出要叠加的图片的对应位置的像素值, 和底层图像的 像素值进行一个混合。 混合的方式有很多种, 但是要注意 alpha 通道的值要转化为 float 类型 (归一化)。最终产生的图像注意还是 3 通道的,搞成 4 通道的话 imshow 就显示错误了, 再说混合好的图像也不再需要 alpha 通道了分享:6 喜欢 1 赠金笔阅读(1951)┊ 评论 (1)┊ 收藏(0) ┊转载(0) ┊ 喜欢 ┊打印┊举报前一篇:OpenCL 性能探讨之 if 后一篇:博客-微博-微信,是怎么的一条路呢~评论 重要提示:警惕虚假中奖信息 [发评论]?元帅2.3 以前版本用这个公式就可以了: DisplayColor = 5 - alpha) / SourceColor x alpha / 255 + 255 BackgroundColor x (25一个嵌套循环遍历 CV_IMAGE_ELEM 就行。 21:43
4.利用 OpenCV产生一个图像,尺寸为 200*240,三通道,其中某一块为红色, 其它皆为黑色,示例图如下。 图 1示例图 4 实验二点处理一、实验目的 掌握灰度直方图...数字图像处理 系专班姓成别: 业: 级: 计算机系...利用 OpenCV 读入一个图像文件,并将其显示到屏幕上...// 创 建位单通道浮点数 // copy A to dft_A...OpenCV Mat颜色通道分离,各通道合图显示_计算机软件及应用_IT/计算机_专业资料。OpenCV 2.4.3 C++ Mat 颜色通道分离(split)合成(merge),各通道合并到一个图像显示...int nSize int ID int nChannels int alphaChannel ...不用考虑通道的个数,因为你要赋值给获取 Mat::at...( &opencv.png& ); while(1) { imshow( &show...opencv 检测直线、线段、圆、矩形_IT/计算机_专业资料...argv[1] : &pic1.png&; IplImage* src = ...【OpenCV 入门教程之五】分离颜色通道&多通道图像混合上篇文章中我们讲到了使用 addWeighted 函数进行图像混合操作, 以及将 ROI 和 addWeighted 函数结合起来使用,对...但是 opencv 用 imread(opencv 读图的函数)读进来的图像,三通道存放顺序为 B、...下面的代码把红色通道值大于128的颜色的置为白色,左边为原图,右边为处理 过后的... 18:35 689 人阅读 评论(0) 收藏 举报 opencv 图像处理 目录(?...5. Mat image=imread(&../shape.png&); 6. cvtColor(image,image,CV_BGR...Opencv 里的函数,可以不注册电脑) ,在“解决方案资源管理器”窗口下, 右击, ...编译运行下面的程序需要将 rice.png 文件放在项目目录下,即与项目文件同 一个...3. 利用 OpenCV 实现了读入一个图像文件并将其显示...?试举几个图像处理应用的例子 图像处理的应用包括:...OpenCV 产生一个图像,尺寸为 200*240,三通道,其中...
All rights reserved Powered by
copyright &copyright 。文档资料库内容来自网络,如有侵犯请联系客服。python - scipy.misc.imread returns object with no size/shape - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 7.0 million programmers, just like you, helping each other.
J it only takes a minute:
I have a question that is similar to , though not identical. My goal is to read a TIFF image using scipy.misc.imread and then use the array of grayscale values that is returned by the function. When I do this for one images of dimensions
px, I get what I need. However, when I attempt the same for a larger image ( px), imread returns the following object:
&PIL.TiffImagePlugin.TiffImageFile image mode=LA size= at 0x3906B48&
I would like to extract the data out of this object, for instance using .getdata(), but the object itself does not seem to have any shape or size, as was the case in the question I linked above. Ben then proposed the following solution:
pip uninstall PIL
brew install libjpeg
pip install PIL
However, I'm working with a TIFF file, rather than JPEG (libtiff appears to be installed). Also, reading a smaller image works fine, whereas reading a big one suddenly causes problems. Does anyone have any idea of what is going on?
Scipy is calling np.array, but when it fails to see __array__, creates an object array instead. You should use raw PIL and read the information from there, there are many questions in SO on how to convert a PIL object into a Numpy array.
The reason why it works with smaller ones may be that the size is so big that it makes PIL be careful about memory, but I am not sure.
An alternative that may work would be to use Pillow (a fork of PIL), or
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
rev .25686
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 ios9分屏 的文章

 

随机推荐