opencv python接口 像素访问例程
2014-06-14 21:42
295 查看
opencv2.3.1 python 2.7
import cv2.cv
from decimal import *
from math import ceil
wnd_name = "CV";
cv2.namedWindow(wnd_name);
img = cv2.imread("a.jpg");
cv2.imshow(wnd_name,img);
key = cv2.waitKey(-1);
mB = img[0:img.shape[0], 0:img.shape[1], 0] / 255.0;
mG = img[0:img.shape[0], 0:img.shape[1], 1] / 255.0;
mR = img[0:img.shape[0], 0:img.shape[1], 2] / 255.0;
mY = 65.481 * mR + 128.553 * mG + 24.966 * mB + 16;
#round to int
mY = mY + 0.5;
mY = mY.astype(int);
for row in range(img.shape[0] - 1) :
for col inrange(img.shape[1] - 1) :
if mY[row,col] > 255:
mY[row,col] = 255;
img[:,:,0] = mY;
img[:,:,1] = mY;
img[:,:,2] = mY;
#
#for row in range(img.shape[0]) :
# for col inrange(img.shape[1]) :
# B = img[row,col,0];
# G = img[row,col,1];
# R = img[row,col,2];
# B = B / 255.0;
# G = G / 255.0;
# R = R / 255.0;
# Y = 65.481 * R + 128.553 * G + 24.966 * B +16;
# Y = int(ceil(Y));
# if Y > 255 :
# Y =255;
# if Y < 0 :
# Y =0;
# img[row,col,0] = Y;
# img[row,col,1] = Y;
# img[row,col,2] = Y;
#
cv2.imshow(wnd_name,img);
key = cv2.waitKey(-1);
while key == 'q' :
key =cv2.waitKey(1);
cv2.destroyAllWindows();
cv2.imwrite("result.jpg", img);
最后, python逐像素访问效率很低,最好使用矩阵运算
import cv2.cv
from decimal import *
from math import ceil
wnd_name = "CV";
cv2.namedWindow(wnd_name);
img = cv2.imread("a.jpg");
cv2.imshow(wnd_name,img);
key = cv2.waitKey(-1);
mB = img[0:img.shape[0], 0:img.shape[1], 0] / 255.0;
mG = img[0:img.shape[0], 0:img.shape[1], 1] / 255.0;
mR = img[0:img.shape[0], 0:img.shape[1], 2] / 255.0;
mY = 65.481 * mR + 128.553 * mG + 24.966 * mB + 16;
#round to int
mY = mY + 0.5;
mY = mY.astype(int);
for row in range(img.shape[0] - 1) :
for col inrange(img.shape[1] - 1) :
if mY[row,col] > 255:
mY[row,col] = 255;
img[:,:,0] = mY;
img[:,:,1] = mY;
img[:,:,2] = mY;
#
#for row in range(img.shape[0]) :
# for col inrange(img.shape[1]) :
# B = img[row,col,0];
# G = img[row,col,1];
# R = img[row,col,2];
# B = B / 255.0;
# G = G / 255.0;
# R = R / 255.0;
# Y = 65.481 * R + 128.553 * G + 24.966 * B +16;
# Y = int(ceil(Y));
# if Y > 255 :
# Y =255;
# if Y < 0 :
# Y =0;
# img[row,col,0] = Y;
# img[row,col,1] = Y;
# img[row,col,2] = Y;
#
cv2.imshow(wnd_name,img);
key = cv2.waitKey(-1);
while key == 'q' :
key =cv2.waitKey(1);
cv2.destroyAllWindows();
cv2.imwrite("result.jpg", img);
最后, python逐像素访问效率很低,最好使用矩阵运算
相关文章推荐
- openCV—Python(3)—— 访问与操作像素
- OpenCV-Python:访问像素值并修改、访问图片属性、设置ROI、分割合并图片
- python OpenCV 图像像素访问 (三)
- 6-python图像处理opencv(2.图像像素访问,通道分离与合并)
- [链接]OpenCV中的python接口
- 像素临近区域、颜色通道、ROI区域的访问---opencv.2学习笔记2
- 关于opencv访问像素点的疑问已解决
- Python中常用数据库访问接口模块
- opencv访问图像像素
- OpenCV访问图像像素值的若干种方式
- OpenCV: cv::Mat图像像素值访问及其坐标系问题
- 利用OpenCV 2.2 的Python接口实现Ostu(大津法)获取阈值
- OpenCV的图像像素访问[转]
- OpenCV之像素访问优化
- opencv OpenCV访问图像像素, 数组元素等方法收集
- 【OpenCV】访问Mat图像中每个像素的值
- OpenCV2 访问各个像素点的方法
- OpenCV访问图像像素, 数组元素等方法收集
- kinect学习笔记四(距离变换 distance tranform 和opencv像素访问方式)
- OpenCV之像素访问(2)