opencv python 每个pixel增加一个数

来源:互联网 发布:爱知超声波流量计软件 编辑:程序博客网 时间:2024/06/14 17:35

在C++里面是这样:https://docs.opencv.org/2.4/doc/tutorials/core/basic_linear_transform/basic_linear_transform.html

#include <cv.h>#include <highgui.h>#include <iostream>using namespace cv;double alpha; /**< Simple contrast control */int beta;  /**< Simple brightness control */int main( int argc, char** argv ){ /// Read image given by user Mat image = imread( argv[1] ); Mat new_image = Mat::zeros( image.size(), image.type() ); /// Initialize values std::cout<<" Basic Linear Transforms "<<std::endl; std::cout<<"-------------------------"<<std::endl; std::cout<<"* Enter the alpha value [1.0-3.0]: ";std::cin>>alpha; std::cout<<"* Enter the beta value [0-100]: "; std::cin>>beta; /// Do the operation new_image(i,j) = alpha*image(i,j) + beta for( int y = 0; y < image.rows; y++ )    { for( int x = 0; x < image.cols; x++ )         { for( int c = 0; c < 3; c++ )              {      new_image.at<Vec3b>(y,x)[c] =         saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );             }    }    } /// Create Windows namedWindow("Original Image", 1); namedWindow("New Image", 1); /// Show stuff imshow("Original Image", image); imshow("New Image", new_image); /// Wait until user press some key waitKey(); return 0;}


在Python里面Mat储存的是np.uint8的numpy数组类型,如果直接用这样的数组进行运算,会有很多意想不到的情况发生

所以最好的方法是先把np.uint8转为int的dtype,然后进行加减乘除运算,完了再转为np.uint8

img0 = cv2.imread(f).astype(int)img = img0*alpha + betaimg[img>255] = 255cv2.imwrite(。。。。。。。)


阅读全文
0 0
原创粉丝点击