python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Opencv图像操作

Opencv简单图像操作方法实战

作者:stoAir

OpenCV是一个开源的计算机视觉库,它提供了一系列丰富的图像处理和计算机视觉算法,这篇文章主要给大家介绍了关于Opencv简单图像操作方法的相关资料,需要的朋友可以参考下

Opencv

一、读取图片

1.imshow

Mat imread(const string& filename, intflags=1 );

flags:
enum
{
/* 8bit, color or not */
   CV_LOAD_IMAGE_UNCHANGED  =-1,
/* 8bit, gray */
   CV_LOAD_IMAGE_GRAYSCALE  =0,
/* ?, color */
   CV_LOAD_IMAGE_COLOR      =1,
/* any depth, ? */
   CV_LOAD_IMAGE_ANYDEPTH   =2,
/* ?, any color */
   CV_LOAD_IMAGE_ANYCOLOR   =4
};

Mat image0=imread("dota.jpg",CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);//载入最真实的图像
Mat image1=imread("dota.jpg",0);//载入灰度图
Mat image2=imread("dota.jpg",199);//载入3通道的彩色图像
Mat logo=imread("dota_logo.jpg");//载入3通道的彩色图像

2.namedWindow

void namedWindow(const string& winname,int flags=WINDOW_AUTOSIZE ); 

3.imshow

void imshow(const string& winname, InputArray mat);

4.效果图

c++

python

二、图像操作

python

import cv2 as cv
import numpy as np
image = cv.imread("/home/stoair/Opencv/Images/1.png", 4)
cv.namedWindow("initial",cv.WINDOW_NORMAL)
cv.namedWindow("after",cv.WINDOW_NORMAL)
height = image.shape[0]
width = image.shape[1]
cv.imshow("initial",image)
for i in range(height):
	for j in range(width):
		(b, g, r) = image[i, j]
		ave = b/3 + g/3 + r/3
		image[i, j] = (ave, ave, ave)

cv.imshow("after",image) 
cv.waitKey(0)

c++

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include <opencv2/imgcodecs.hpp> 
#include <opencv2/opencv.hpp>

using namespace cv;

int main()
{
    Mat image0 = imread("/home/stoair/Opencv/Images/1.png", 4);
    namedWindow("initial", WINDOW_NORMAL);
    namedWindow("change", WINDOW_NORMAL);
    imshow("initial", image0);

    int height = image0.rows;
    int width = image0.cols;
    for(int i = 0; i < height; i++){
        for(int j = 0; j < width; j++){
            unsigned char b = image0.at<Vec3b>(i, j)[0];
            unsigned char g = image0.at<Vec3b>(i, j)[1];
            unsigned char r = image0.at<Vec3b>(i, j)[2];
            unsigned char ave = b/3 + g/3 +r/3;
            image0.at<Vec3b>(i, j)[0] = ave;
            image0.at<Vec3b>(i, j)[1] = ave;
            image0.at<Vec3b>(i, j)[2] = ave;
        }
    }

    imshow("change", image0);
    waitKey(0);
    return 0;
}

总结 

到此这篇关于Opencv简单图像操作方法的文章就介绍到这了,更多相关Opencv图像操作内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文