计算图像像素能量

Seam Carving智能缩放算法的第一步就是图像能量的计算。

#include "stdafx.h"
#include
#include"opencv2/highgui/highgui.hpp"
#include
#include
#include

using namespace std;
using namespace cv;

void SeamCarvercomputeFullEnergy(Mat&image,Mat &energy) {
	//确保能量矩阵与图像匹配
	energy.create(image.rows, image.cols, CV_32S);

	//由第2行开始计算
	for (int i = 1; i < image.rows - 1; ++i) {
		uchar* prev = image.ptr(i - 1);	//Pointer to previous row
		uchar* curr = image.ptr(i);		//Pointer to current row
		uchar* next = image.ptr(i + 1);	//Pointer to next row

		for (int j = 1; j < image.cols - 1; ++j) {
			int val = 0;
			//Energy along the x-axis
			val += (prev[3 * j] - next[3 * j]) * (prev[3 * j] - next[3 * j]);
			val += (prev[3 * j + 1] - next[3 * j + 1]) * (prev[3 * j + 1] - next[3 * j + 1]);
			val += (prev[3 * j + 2] - next[3 * j + 2]) * (prev[3 * j + 2] - next[3 * j + 2]);

			//Energy along the y-axis
			val += (curr[3 * j + 3] - curr[3 * j - 3]) * (curr[3 * j + 3] - curr[3 * j - 3]);
			val += (curr[3 * j + 4] - curr[3 * j - 2]) * (curr[3 * j + 4] - curr[3 * j - 2]);
			val += (curr[3 * j + 5] - curr[3 * j - 1]) * (curr[3 * j + 5] - curr[3 * j - 1]);

			energy.at(i, j) = val;
		}
	}
}
int main()
{
	Mat image, energy;
	image = imread("1.jpg");

	SeamCarvercomputeFullEnergy(image, energy);
	
	imshow("原图", image);
	imshow("Energy", energy);
	
	waitKey();

	

	
}
	

 

你可能感兴趣的:(计算图像像素能量)