ColorReduce

static void help(char* progName)
{
    printf("ColorReduce By cut X bits off from BGR \n Usage:\n %s <imagePath> B G R\n",progName);
    getchar();
}

int main(int argc, char** argv)
{
    if(argc != 5)
    {
        help(argv[0]);
        return -1;
    }

    Mat img = imread(argv[1], CV_LOAD_IMAGE_COLOR);
    if(img.empty() )
    {
        printf("Can't read the image\n");
        return -1;
    }
    if(img.channels()!=3)
    {
        printf("img.channels not 3!\n");
        return -1;
    }

    int bitsB; sscanf( argv[2],"%d",&bitsB);
    int bitsG; sscanf( argv[3],"%d",&bitsG);
    int bitsR; sscanf( argv[4],"%d",&bitsR);

    Mat imgColorReduce(img.rows,img.cols,CV_8UC3);

    for(int i=0;i<img.rows;i++)
    {
        for(int j=0;j<img.cols;j++)
        {
            imgColorReduce.at<cv::Vec3b>(i,j)[0]= ((img.at<cv::Vec3b>(i,j)[0]>>bitsB)<<bitsB);
            imgColorReduce.at<cv::Vec3b>(i,j)[1]= ((img.at<cv::Vec3b>(i,j)[1]>>bitsG)<<bitsG);
            imgColorReduce.at<cv::Vec3b>(i,j)[2]= ((img.at<cv::Vec3b>(i,j)[2]>>bitsR)<<bitsR);
        }
    }
    
    cv::imwrite("src.jpg",img);
    cv::imwrite("out.jpg",imgColorReduce);    
    //waitKey(0);
    return 0;
}

 

posted @ 2018-03-22 00:15  sky20080101  阅读(270)  评论(0)    收藏  举报