实验1.2
#include <bits/stdc++.h> #define inf 2333333333333333 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(int i=a;i<=b;++i) typedef unsigned char uchar; //by war //2020.9.10 using namespace std; using namespace cv; uchar b[10000010]; Mat image0,image1; vector<Mat> v; int pos; void in(int &x){ int y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } void o(int x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } uchar* get_pixel(const uchar *img, int x, int y,int step){ return (uchar*)(img+y*step+x*3); } void getChannel(const uchar *input, int width, int height, int inStep, int inChannels, int channelToGet ){ pos=0; For(i,0,height-1) For(j,0,width-1){ auto k=get_pixel(input,j,i,inStep); b[pos++]=*(k+channelToGet); } } signed main(){ image0=imread("/Users/war/Downloads/dog.PNG"); For(i,0,2){ v.clear(); For(i,1,3) v.push_back(Mat::zeros(image0.cols, image0.rows, CV_8UC1)); getChannel(image0.data, image0.cols, image0.rows, (int)image0.step, image0.channels(),i); Mat image (image0.cols, image0.rows, CV_8UC1, b, (int)image0.step/3); v[i]=image; merge(v,image); imshow("dog",image); waitKey(-1); } imshow("dog",image0); waitKey(-1); return 0; }