#include"highgui.h"
#include"cv.h"
#include"stdio.h"
#include"Saliency.h"
using namespace cv;
#define delNUM 50
//锐化滤波得到显著图
Mat getSrcEnergy(Mat image){
Mat image_gray;
//均值滤波
Mat mask1 = (Mat_<float>(3, 3) << 0, 0, 0, 0, 1, -1, 0, 0, 0);
Mat mask2 = (Mat_<float>(3, 3) << 0, 0, 0, 0, 1, 0, 0, -1, 0);
Mat result(image.rows, image.cols, CV_32F, Scalar(0));
Mat result1(image.rows, image.cols, CV_32F, Scalar(0));
Mat result2(image.rows, image.cols, CV_32F, Scalar(0));
cvtColor(image, image_gray, CV_BGR2GRAY);
filter2D(image_gray, result1, result1.depth(), mask1);
filter2D(image_gray, result2, result2.depth(), mask2);
add(abs(result1), abs(result2), result);
return result;
}
//Saliency算法得到显著图
Mat getEnergy(Mat image){
Saliency sal;
vector<double> salmap(0);
vector<unsigned int> inputImg;
IplImage imgTmp = image;
IplImage* img = cvCloneImage(&imgTmp);
for (int h = 0; h < img->height; h++){
unsigned char*p = (unsigned char*)img->imageData + h*img->widthStep;
for (int w = 0; w < img->width; w++){
unsigned int t = 0;
t += *p++;
t <<= 8;
t += *p++;
t <<= 8;
t += *p++;
inputImg.push_back(t);
}
}
sal.GetSaliencyMap(inputImg, img->width, img->height, salmap);
int index = 0;
IplImage* imageOut = cvCreateImage(cvGetSize(img), IPL_DEPTH_64F, 1);
IplImage* ori = cvCreateImage(cvGetSize(img), IPL_DEPTH_32F, 1);
for (int h = 0; h < imageOut->height; h++){
double *p = (double*)(imageOut->imageData + h*imageOut->widthStep);
for (int w = 0; w < imageOut->width; w++){
*p++ = salmap[index++];
}
}
cvConvertScale(imageOut, ori);
Mat imgMat(ori, 1);
cvReleaseImage(&img);
cvReleaseImage(&imageOut);
return imgMat;
}
//累计能量,范围为3个
void calculateEnergy3(Mat image,Mat &EnergyMat,Mat &trace){
image.copyTo(EnergyMat);
for (int i = 1; i < image.rows; i++){
//第一列的能量累计
if (EnergyMat.at<float>(i - 1, 0) <= EnergyMat.at<float>(i - 1, 1)){
EnergyMat.at<float>(i, 0) = image.at<float>(i, 0) + EnergyMat.at<float>(i - 1, 0);
trace.at<float>(i, 0) = 0; //正上方的是0
}
else{
EnergyMat.at<float>(i, 0) = image.at<float>(i, 0) + EnergyMat.at<float>(i - 1, 1);
trace.at<float>(i, 0) = 1; //右上方的是1
}
//中间列的能量累计
for (int j = 1; j < image.cols - 1; j++){
float en[3], min;
int index = -1;
en[0] = EnergyMat.at<float>(i - 1, j - 1);
en[1] = EnergyMat.at<float>(i - 1, j);
en[2] = EnergyMat.at<float>(i - 1, j + 1);
min = en[0];
for (int k = 1; k < 3; k++){
if (en[k] < min){
min = en[k];
index = k - 1;
}
}
EnergyMat.at<float>(i, j) = image.at<float>(i, j) + EnergyMat.at<float>(i - 1, j + index);
trace.at<float>(i, j) = index;
}
//最后一列的能量累计
if (EnergyMat.at<float>(i - 1, image.cols - 1) <= EnergyMat.at<float>(i - 1, image.cols - 2)){
EnergyMat.at<float>(i, image.cols - 1) = image.at<float>(i, image.cols - 1) + EnergyMat.at<float>(i - 1, image.cols - 1);
trace.at<float>(i, image.cols - 1) = 0; //正上方的是0
}
else{
EnergyMat.at<float>(i, image.cols - 1) = image.at<float>(i, image.cols - 1) + EnergyMat.at<float>(i - 1, image.cols - 2);
trace.at<float>(i, image.cols - 1) = -1; //左上方的是-1
}
}
}
//找到能量最小的路线,范围为3个
void findMinEnergyTrace3(Mat &EnergyMat,Mat &traceMat,Mat &minTrace){
int index = 0;
float minEnergyInLastRow;
minEnergyInLastRow = EnergyMat.at<float>(EnergyMat.rows - 1, 0);
//在最后一排找出累计能量最小的路线
for (int j = 1; j < EnergyMat.cols; j++){
if (EnergyMat.at<float>(EnergyMat.rows - 1, j) < minEnergyInLastRow){
minEnergyInLastRow = EnergyMat.at<float>(EnergyMat.rows - 1, j);
index = j;
}
}
minTrace.at<float>(EnergyMat.rows - 1, 0) = index;
int tmpIndex = index;
for (int i = EnergyMat.rows - 1; i > 0; i--)
{
int temp = traceMat.at<float>(i, tmpIndex);// 当前位置traceMat所存的值
if (temp == -1) // 往左走
{
tmpIndex = tmpIndex - 1;
}
else if (temp == 1) // 往右走
{
tmpIndex = tmpIndex + 1;
} // 如果temp = 1,则往正上走,tmpIndex不需要做修改
minTrace.at<float>(i - 1, 0) = tmpIndex;
}
}
//累计能量,范围为5个
void calculateEnergy5(Mat image, Mat &EnergyMat, Mat &trace){
image.copyTo(EnergyMat);
//第1,2列的能量累计
for (int i = 1; i < image.rows; i++){
for (int j = 0; j < 2; j++){
float en[3], min;
int index = 0;
en[0] = EnergyMat.at<float>(i - 1, j); //正上方的是0
en[1] = EnergyMat.at<float>(i - 1, j + 1);//右一上方的是1
en[2] = EnergyMat.at<float>(i - 1, j + 2);//右二上方的是0
min = en[0];
for (int k = 1; k < 3; k++){
if (en[k] < min){
min = en[k];
index = k;
}
}
EnergyMat.at<float>(i, j) = image.at<float>(i, j) + EnergyMat.at<float>(i - 1, j + index);
trace.at<float>(i, j) = index;
}
//中间列的能量累计
for (int j = 2; j < image.cols - 2; j++){
float en[5], min;
int index = -2;
en[0] = EnergyMat.at<float>(i - 1, j - 2);
en[1] = EnergyMat.at<float>(i - 1, j - 1);
en[2] = EnergyMat.at<float>(i - 1, j);
en[3] = EnergyMat.at<float>(i - 1, j + 1);
en[4] = EnergyMat.at<float>(i - 1, j + 2);
min = en[0];
for (int k = 1; k < 5; k++){
if (en[k] < min){
min = en[k];
index = k - 2;
}
}
EnergyMat.at<float>(i, j) = image.at<float>(i, j) + EnergyMat.at<float>(i - 1, j + index);
trace.at<float>(i, j) = index;
}
//最后两列的能量累计
for (int j = image.cols - 2; j < image.cols; j++){
float en[3], min;
int index = -2;
en[0] = EnergyMat.at<float>(i - 1, j - 2); //左一上方的是-2
en[1] = EnergyMat.at<float>(i - 1, j - 1); //左二上方的是-1
en[2] = EnergyMat.at<float>(i - 1, j); //正上方的是0
min = en[0];
for (int k = 1; k < 3; k++){
if (en[k] < min){
min = en[k];
index = k - 2;
}
}
EnergyMat.at<float>(i, j) = image.at<float>(i, j) + EnergyMat.at<float>(i - 1, j + index);
trace.at<float>(i, j) = index;
}
}
}
//找到能量最小的路线,范围为5个
void findMinEnergyTrace5(Mat &EnergyMat, Mat &traceMat, Mat &minTrace){
int index = 0;
float minEnergyInLastRow;
minEnergyInLastRow = EnergyMat.at<float>(EnergyMat.rows - 1, 0);
//在最后一排找出累计能量最小的路线
for (int j = 1; j < EnergyMat.cols; j++){
if (EnergyMat.at<float>(EnergyMat.rows - 1, j) < minEnergyInLastRow){
minEnergyInLastRow = EnergyMat.at<float>(EnergyMat.rows - 1, j);
index = j;
}
}
minTrace.at<float>(EnergyMat.rows - 1, 0) = index;
int tmpIndex = index;
for (int i = EnergyMat.rows - 1; i > 0; i--){
int temp = traceMat.at<float>(i, tmpIndex);// 当前位置traceMat所存的值
if (temp == -2){
tmpIndex = tmpIndex - 2;// 往左走两步
}
else if (temp == -1){
tmpIndex = tmpIndex - 1;// 往左走
}
else if (temp == 1){
tmpIndex = tmpIndex + 1;// 往右走
}
else if (temp == 2){
tmpIndex = tmpIndex + 2;// 往右走两步
}// 如果temp = 1,则往正上走,tmpIndex不需要做修改
minTrace.at<float>(i - 1, 0) = tmpIndex;
}
}
//删除能量最低的路线
void deleteCol(Mat image, Mat &OutputImage, Mat minEnergyTrace){
for (int i = 0; i < OutputImage.rows; i++){
int delMark = minEnergyTrace.at<float>(i, 0);
//在要删除的路线前方的像素直接复制
for (int j = 0; j < delMark; j++){
OutputImage.at<Vec3b>(i, j)[0] = image.at<Vec3b>(i, j)[0];
OutputImage.at<Vec3b>(i, j)[1] = image.at<Vec3b>(i, j)[1];
OutputImage.at<Vec3b>(i, j)[2] = image.at<Vec3b>(i, j)[2];
}
//在要删除的路线后方像素往前复制一格
for (int j = delMark; j < OutputImage.cols; j++){
OutputImage.at<Vec3b>(i, j)[0] = image.at<Vec3b>(i, j + 1)[0];
OutputImage.at<Vec3b>(i, j)[1] = image.at<Vec3b>(i, j + 1)[1];
OutputImage.at<Vec3b>(i, j)[2] = image.at<Vec3b>(i, j + 1)[2];
}
}
}
//循环NUM次,实现缩小NUM像素
Mat update(Mat image,int times,int sal){
Mat E
没有合适的资源?快使用搜索试试~ 我知道了~
SeamCarving opencv c++
共45个文件
tlog:12个
jpg:9个
pdb:4个
需积分: 9 42 下载量 165 浏览量
2016-11-17
22:15:27
上传
评论 3
收藏 8.02MB ZIP 举报
温馨提示
分别用了基于梯度图和Saliency算法的显著度图计算方法,以水平和垂直两种方式简单地压缩图片。实现了有关动态规划算法
资源推荐
资源详情
资源评论
收起资源包目录
final.zip (45个子文件)
final
Release
final.pdb 1.32MB
final.exe 39KB
final.sdf 14.75MB
final.sln 961B
urlmon.pdb
3CED794C142C4A1EA5CBE3DE217327831
urlmon.pd_ 0B
final.v12.suo 25KB
wuser32.pdb
002B6CB5B9D446498DF80DC22A41E3161
final
8.jpg 113KB
final.vcxproj 4KB
Release
Saliency.obj 1.13MB
vc120.pdb 1.07MB
final.log 3KB
main.obj 1.96MB
final.tlog
final.lastbuildstate 173B
CL.write.1.tlog 642B
CL.read.1.tlog 20KB
cl.command.1.tlog 2KB
link.write.1.tlog 422B
link.command.1.tlog 12KB
link.read.1.tlog 7KB
Saliency.h 2KB
2.jpg 74KB
1.jpg 51KB
6.jpg 62KB
3.jpg 90KB
5.jpg 200KB
Saliency.cpp 6KB
4.jpg 252KB
main.cpp 10KB
9.jpg 69KB
7.jpg 93KB
Debug
vc120.pdb 1.2MB
final.log 2KB
main.obj 408KB
vc120.idb 603KB
final.tlog
final.lastbuildstate 171B
CL.write.1.tlog 412B
CL.read.1.tlog 13KB
cl.command.1.tlog 616B
link.write.1.tlog 390B
link.command.1.tlog 3KB
link.read.1.tlog 6KB
final.vcxproj.filters 1KB
Debug
final.pdb 1.68MB
final.exe 110KB
final.ilk 563KB
共 45 条
- 1
资源评论
fantasy4335
- 粉丝: 2
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功