#include <iostream>
#include "opencv2\opencv.hpp"
using namespace std;
using namespace cv;
class Strategy
{
public:
//uchar *RGB;
int mean,_count,k;
virtual ~Strategy() {}
virtual Mat process_image(Mat &image) = 0;
};
class Context
{
public:
Mat ContextInterface(Strategy *pStrategy,Mat &image);
};
class original : public Strategy
{
public:
virtual ~original() {}
virtual Mat process_image(Mat &image);
};
class gray : public Strategy
{
public:
virtual ~gray() {}
virtual Mat process_image(Mat &image);
};
class threshhold : public Strategy
{
public:
virtual ~threshhold() {}
virtual Mat process_image(Mat &image);
};
Mat Context::ContextInterface(Strategy *pStrategy,Mat &image)
{
return pStrategy->process_image(image);
}
Mat original::process_image(Mat &image)
{
return image;
}
Mat gray::process_image(Mat &image)
{
mean=_count=0;
for(int i=0; i<image.rows; i++)
{
for(int j=0; j<image.cols; j++)
{
k=0.333*(image.at<Vec3b>(i,j)[2]+image.at<Vec3b>(i,j)[1]+image.at<Vec3b>(i,j)[0]);
image.at<Vec3b>(i,j)[2]=image.at<Vec3b>(i,j)[1]=image.at<Vec3b>(i,j)[0]=k;
mean+=k;
++_count;
}
}
return image;
}
Mat threshhold::process_image(Mat &image)
{
for(int i=0; i<image.rows; i++)
{
for(int j=0; j<image.cols; j++)
{
if(image.at<Vec3b>(i,j)[2]<mean)
{
image.at<Vec3b>(i,j)[2]=image.at<Vec3b>(i,j)[1]=image.at<Vec3b>(i,j)[0]=0;
}
else
{
image.at<Vec3b>(i,j)[2]=image.at<Vec3b>(i,j)[1]=image.at<Vec3b>(i,j)[0]=255;
}
}
}
return image;
}
int main(void)
{
Mat image=imread("pic3.png", CV_LOAD_IMAGE_COLOR); // Read the file
Context* pContext = new Context();
Strategy* pStrategy;
int mean;
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
cout<<image.rows;
//show original picture
pStrategy = new original();
namedWindow( "original", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "original", pContext->ContextInterface(pStrategy,image) ); // Show our image inside it.
delete pStrategy;
//show gray picture
pStrategy = new gray();
namedWindow( "gray", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "gray", pContext->ContextInterface(pStrategy,image) ); // Show our image inside it.
mean=(pStrategy->mean)/(pStrategy->_count);
delete pStrategy;
//show threshhold picture
pStrategy = new threshhold();
(pStrategy->mean)=mean;
namedWindow( "threshhold", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "threshhold", pContext->ContextInterface(pStrategy,image) ); // Show our image inside it.
delete pStrategy;
waitKey(0); // Wait for a keystroke in the window
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSAib3BlbmN2MlxvcGVuY3YuaHBwIgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdXNpbmcgbmFtZXNwYWNlIGN2OwoKY2xhc3MgU3RyYXRlZ3kKewpwdWJsaWM6CiAgICAvL3VjaGFyICpSR0I7CiAgICBpbnQgbWVhbixfY291bnQsazsKICAgIHZpcnR1YWwgflN0cmF0ZWd5KCkge30KICAgIHZpcnR1YWwgTWF0IHByb2Nlc3NfaW1hZ2UoTWF0ICZpbWFnZSkgPSAwOwp9OwoKY2xhc3MgQ29udGV4dAp7CnB1YmxpYzoKICAgIE1hdCBDb250ZXh0SW50ZXJmYWNlKFN0cmF0ZWd5ICpwU3RyYXRlZ3ksTWF0ICZpbWFnZSk7Cn07CgpjbGFzcyBvcmlnaW5hbCA6IHB1YmxpYyBTdHJhdGVneQp7CnB1YmxpYzoKICAgIHZpcnR1YWwgfm9yaWdpbmFsKCkge30KCiAgICB2aXJ0dWFsIE1hdCBwcm9jZXNzX2ltYWdlKE1hdCAmaW1hZ2UpOwp9OwoKY2xhc3MgZ3JheSA6IHB1YmxpYyBTdHJhdGVneQp7CnB1YmxpYzoKICAgIHZpcnR1YWwgfmdyYXkoKSB7fQoKICAgIHZpcnR1YWwgTWF0IHByb2Nlc3NfaW1hZ2UoTWF0ICZpbWFnZSk7Cn07CgpjbGFzcyB0aHJlc2hob2x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ICAgICAgICAgICBpZihpbWFnZS5hdDxWZWMzYj4oaSxqKVsyXTxtZWFuKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpbWFnZS5hdDxWZWMzYj4oaSxqKVsyXT1pbWFnZS5hdDxWZWMzYj4oaSxqKVsxXT1pbWFnZS5hdDxWZWMzYj4oaSxqKVswXT0wOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaW1hZ2UuYXQ8VmVjM2I+KGksailbMl09aW1hZ2UuYXQ8VmVjM2I+KGksailbMV09aW1hZ2UuYXQ8VmVjM2I+KGksailbMF09MjU1OwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIGltYWdlOwp9CgoKCmludCBtYWluKHZvaWQpCnsKICAgIE1hdCBpbWFnZT1pbXJlYWQoInBpYzMucG5nIiwgQ1ZfTE9BRF9JTUFHRV9DT0xPUik7ICAgLy8gUmVhZCB0aGUgZmlsZQogICAgQ29udGV4dCogcENvbnRleHQgPSBuZXcgQ29udGV4dCgpOwogICAgU3RyYXRlZ3kqIHBTdHJhdGVneTsKICAgIGludCBtZWFuOwoKICAgIGlmKCEgaW1hZ2UuZGF0YSApICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLy8gQ2hlY2sgZm9yIGludmFsaWQgaW5wdXQKICAgIHsKICAgICAgICBjb3V0IDw8ICAiQ291bGQgbm90IG9wZW4gb3IgZmluZCB0aGUgaW1hZ2UiIDw8IHN0ZDo6ZW5kbCA7CiAgICAgICAgcmV0dXJuIC0xOwogICAgfQoKICAgIGNvdXQ8PGltYWdlLnJvd3M7CgogICAgLy9zaG93IG9yaWdpbmFsIHBpY3R1cmUKICAgIHBTdHJhdGVneSA9IG5ldyBvcmlnaW5hbCgpOwogICAgbmFtZWRXaW5kb3coICJvcmlnaW5hbCIsIENWX1dJTkRPV19BVVRPU0laRSApOy8vIENyZWF0ZSBhIHdpbmRvdyBmb3IgZGlz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