#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main( int argc, char** argv )
{
// read image
Mat img = imread("hand.png");
if (img.empty())
{
cout<<"Read image fail!"<<endl;
return 1;
}
imshow("input", img);
// step 1: initialize parameters
const int MAX_ITERATION = 20;
float h = 20;
float delta = 1 / MAX_ITERATION;
Mat net = Mat::zeros(img.rows, img.cols, CV_32FC1); // F
Mat output= Mat::zeros(img.rows, img.cols, CV_32FC1); // Y
Mat threshold = Mat::ones(img.rows, img.cols, CV_32FC1);// E
Mat image = Mat::zeros(img.rows, img.cols, CV_32FC1); // S
vector<Mat> iteraOutput;
// step 2: normalize image to [0, 1.0]
Mat temp;
cvtColor(img, temp, CV_RGB2GRAY);
double maxP, minP;
minMaxLoc(temp, &minP, &maxP);
temp.convertTo(image, CV_32FC1, 1.0 / (maxP - minP), - minP / (maxP - minP));
// step 3: start iteration
for (int k = 1; k <= MAX_ITERATION; k++)
{
cout<<" Iteration: "<< k <<endl;
// formula (7)
float lamda = 1.0 / (MAX_ITERATION + k);
for (int i = 1; i < image.rows - 1; i++)
{
for (int j = 1; j < image.cols - 1; j++)
{
// formula (6)
float weight = (1 - abs(image.at<float>(i, j) - image.at<float>(i - 1, j))) +
(1 - abs(image.at<float>(i, j) - image.at<float>(i + 1, j))) +
(1 - abs(image.at<float>(i, j) - image.at<float>(i, j - 1))) +
(1 - abs(image.at<float>(i, j) - image.at<float>(i, j + 1))) +
0.5 * (1 - abs(image.at<float>(i, j) - image.at<float>(i - 1, j - 1))) +
0.5 * (1 - abs(image.at<float>(i, j) - image.at<float>(i - 1, j + 1))) +
0.5 * (1 - abs(image.at<float>(i, j) - image.at<float>(i + 1, j - 1))) +
0.5 * (1 - abs(image.at<float>(i, j) - image.at<float>(i + 1, j + 1)));
// formula (4)
net.at<float>(i, j) = net.at<float>(i, j) - lamda + image.at<float>(i, j) + weight;
// formula (5)
threshold.at<float>(i, j) = threshold.at<float>(i, j) - delta + h * output.at<float>(i, j);
// formula (2)
output.at<float>(i, j) = net.at<float>(i, j) > threshold.at<float>(i, j) ? 1.0 : 0.0;
}
}
iteraOutput.push_back(255 * output);
}
// step 4: create a big picture to show all iteration process result
Mat showImg = Mat::zeros(img.rows * 4, img.cols * 5, CV_32FC1);
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
{
Rect roi(img.cols * j, img.rows * i, img.cols, img.rows);
resize(iteraOutput[i * 4 + j], showImg(roi), roi.size());
}
}
imshow("output", showImg);
waitKey(-1);
return 0;
}
#include <iostream>
using namespace std;
using namespace cv;
int main( int argc, char** argv )
{
// read image
Mat img = imread("hand.png");
if (img.empty())
{
cout<<"Read image fail!"<<endl;
return 1;
}
imshow("input", img);
// step 1: initialize parameters
const int MAX_ITERATION = 20;
float h = 20;
float delta = 1 / MAX_ITERATION;
Mat net = Mat::zeros(img.rows, img.cols, CV_32FC1); // F
Mat output= Mat::zeros(img.rows, img.cols, CV_32FC1); // Y
Mat threshold = Mat::ones(img.rows, img.cols, CV_32FC1);// E
Mat image = Mat::zeros(img.rows, img.cols, CV_32FC1); // S
vector<Mat> iteraOutput;
// step 2: normalize image to [0, 1.0]
Mat temp;
cvtColor(img, temp, CV_RGB2GRAY);
double maxP, minP;
minMaxLoc(temp, &minP, &maxP);
temp.convertTo(image, CV_32FC1, 1.0 / (maxP - minP), - minP / (maxP - minP));
// step 3: start iteration
for (int k = 1; k <= MAX_ITERATION; k++)
{
cout<<" Iteration: "<< k <<endl;
// formula (7)
float lamda = 1.0 / (MAX_ITERATION + k);
for (int i = 1; i < image.rows - 1; i++)
{
for (int j = 1; j < image.cols - 1; j++)
{
// formula (6)
float weight = (1 - abs(image.at<float>(i, j) - image.at<float>(i - 1, j))) +
(1 - abs(image.at<float>(i, j) - image.at<float>(i + 1, j))) +
(1 - abs(image.at<float>(i, j) - image.at<float>(i, j - 1))) +
(1 - abs(image.at<float>(i, j) - image.at<float>(i, j + 1))) +
0.5 * (1 - abs(image.at<float>(i, j) - image.at<float>(i - 1, j - 1))) +
0.5 * (1 - abs(image.at<float>(i, j) - image.at<float>(i - 1, j + 1))) +
0.5 * (1 - abs(image.at<float>(i, j) - image.at<float>(i + 1, j - 1))) +
0.5 * (1 - abs(image.at<float>(i, j) - image.at<float>(i + 1, j + 1)));
// formula (4)
net.at<float>(i, j) = net.at<float>(i, j) - lamda + image.at<float>(i, j) + weight;
// formula (5)
threshold.at<float>(i, j) = threshold.at<float>(i, j) - delta + h * output.at<float>(i, j);
// formula (2)
output.at<float>(i, j) = net.at<float>(i, j) > threshold.at<float>(i, j) ? 1.0 : 0.0;
}
}
iteraOutput.push_back(255 * output);
}
// step 4: create a big picture to show all iteration process result
Mat showImg = Mat::zeros(img.rows * 4, img.cols * 5, CV_32FC1);
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
{
Rect roi(img.cols * j, img.rows * i, img.cols, img.rows);
resize(iteraOutput[i * 4 + j], showImg(roi), roi.size());
}
}
imshow("output", showImg);
waitKey(-1);
return 0;
}