图像分割之交叉视觉皮质(ICM)代码

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值