Demo entry 6626433

cut_a_line

   

Submitted by LiangChen on Jun 25, 2017 at 04:55
Language: C++. Code size: 8.5 kB.

void seam_carving_graph::cut_a_line(bool is_height, bool del_obj)
{
	if (is_height == true)
	{
		for (int i = 0; i < temp_h; ++i)
		{
			for (int j = 0; j < temp_w; ++j)
			{
				if (i == 0)
				{
					a[i][j] = energy_graph[i][j];
				}
				else
				{
					a[i][j] = energy_graph[i][j];
					double temp = 100000000.0;
					int ind = 0;
					for (int k = max(0, j - 1); k < min(temp_w, j + 2); ++k)
					{
						if (a[i-1][k] <= temp)
						{
							temp = a[i - 1][k];
							ind = k;
						}
					}
					a[i][j] += temp;
					if (del_obj == 1 && del_img.at<Vec3b>(i, j)[0] < 100)
					{
						a[i][j] -= 1000000;
					}
					b[i][j] = ind;
				}
			}
		}

		double mini = 100000000.0;
		int r = -1;
		int index[6000];

		for (int i = 0; i < temp_w; ++i)
		{
			if (a[temp_h - 1][i] <= mini)
			{
				mini = a[temp_h - 1][i];
				r = i;
			}
		}

		for (int i = temp_h - 1; i >= 1; --i)
		{			
			index[i] = r;
			r = b[i][index[i]];
		}
		index[0] = r;

		for (int i = 0; i < temp_h; ++i)
		{
			h_seam_lines[h_count][i] = index[i];
		}
		h_count += 1;

		temp_w -= 1;

		for (int i = 0; i < temp_h; ++i) //update image
		{
			if (index[i] == 0)
			{
				this->img.at<Vec3b>(i, 0)[0] = (this->img.at<Vec3b>(i, 0)[0] + this->img.at<Vec3b>(i, 1)[0]) / 2;
				this->img.at<Vec3b>(i, 0)[1] = (this->img.at<Vec3b>(i, 0)[1] + this->img.at<Vec3b>(i, 1)[1]) / 2;
				this->img.at<Vec3b>(i, 0)[2] = (this->img.at<Vec3b>(i, 0)[2] + this->img.at<Vec3b>(i, 1)[2]) / 2;
				for (int j = 1; j < temp_w; ++j)
				{
					this->img.at<Vec3b>(i, j)[0] = this->img.at<Vec3b>(i, j + 1)[0];
					this->img.at<Vec3b>(i, j)[1] = this->img.at<Vec3b>(i, j + 1)[1];
					this->img.at<Vec3b>(i, j)[2] = this->img.at<Vec3b>(i, j + 1)[2];
				}
			}
			else if (index[i] == temp_w)
			{
				this->img.at<Vec3b>(i, index[i] - 1)[0] = (this->img.at<Vec3b>(i, index[i] - 1)[0] + this->img.at<Vec3b>(i, index[i])[0]) / 2;
				this->img.at<Vec3b>(i, index[i] - 1)[1] = (this->img.at<Vec3b>(i, index[i] - 1)[1] + this->img.at<Vec3b>(i, index[i])[1]) / 2;
				this->img.at<Vec3b>(i, index[i] - 1)[2] = (this->img.at<Vec3b>(i, index[i] - 1)[2] + this->img.at<Vec3b>(i, index[i])[2]) / 2;
			}
			else
			{
				this->img.at<Vec3b>(i, index[i] - 1)[0] = (this->img.at<Vec3b>(i, index[i] - 1)[0] + this->img.at<Vec3b>(i, index[i])[0]) / 2;
				this->img.at<Vec3b>(i, index[i] - 1)[1] = (this->img.at<Vec3b>(i, index[i] - 1)[1] + this->img.at<Vec3b>(i, index[i])[1]) / 2;
				this->img.at<Vec3b>(i, index[i] - 1)[2] = (this->img.at<Vec3b>(i, index[i] - 1)[2] + this->img.at<Vec3b>(i, index[i])[2]) / 2;
				this->img.at<Vec3b>(i, index[i])[0] = (this->img.at<Vec3b>(i, index[i] + 1)[0] + this->img.at<Vec3b>(i, index[i])[0]) / 2;
				this->img.at<Vec3b>(i, index[i])[1] = (this->img.at<Vec3b>(i, index[i] + 1)[1] + this->img.at<Vec3b>(i, index[i])[1]) / 2;
				this->img.at<Vec3b>(i, index[i])[2] = (this->img.at<Vec3b>(i, index[i] + 1)[2] + this->img.at<Vec3b>(i, index[i])[2]) / 2;

				for (int j = index[i] + 1; j < temp_w; ++j)
				{
					this->img.at<Vec3b>(i, j)[0] = this->img.at<Vec3b>(i, j + 1)[0];
					this->img.at<Vec3b>(i, j)[1] = this->img.at<Vec3b>(i, j + 1)[1];
					this->img.at<Vec3b>(i, j)[2] = this->img.at<Vec3b>(i, j + 1)[2];
					if (del_obj == 1)
					{
						this->del_img.at<Vec3b>(i, j)[0] = this->del_img.at<Vec3b>(i, j + 1)[0];
						this->del_img.at<Vec3b>(i, j)[1] = this->del_img.at<Vec3b>(i, j + 1)[1];
						this->del_img.at<Vec3b>(i, j)[2] = this->del_img.at<Vec3b>(i, j + 1)[2];
					}
				}
			}
		}

		
		for (int i = 0; i < temp_h; ++i) //update energy image
		{
			if (index[i] == 0)
			{
				energy_graph[i][0] = calculate_energy(i, 0);
				for (int j = 1; j < temp_w; ++j)
				{
					energy_graph[i][j] = energy_graph[i][j + 1];
				}
			}
			else if (index[i] == temp_w)
			{
				energy_graph[i][index[i] - 1] = calculate_energy(i, index[i] - 1);
			}
			else
			{
				energy_graph[i][index[i] - 1] = calculate_energy(i, index[i] - 1);
				energy_graph[i][index[i]] = calculate_energy(i, index[i]);
				for (int j = index[i] + 1; j < temp_w; ++j)
				{
					energy_graph[i][j] = energy_graph[i][j + 1];
				}
			}
		}
		return;
	}
	else
	{
		for (int i = 0; i < temp_w; ++i)
		{
			for (int j = 0; j < temp_h; ++j)
			{
				if (i == 0)
				{
					a[j][i] = energy_graph[j][i];
				}
				else
				{
					a[j][i] = energy_graph[j][i];
					double temp = 100000000.0;
					int ind = 0;
					for (int k = max(0, j - 1); k < min(temp_h, j + 2); ++k)
					{
						if (a[k][i - 1] <= temp)
						{
							temp = a[k][i - 1];
							ind = k;
						}
					}
					a[j][i] += temp;
					if (del_obj == 1 && del_img.at<Vec3b>(j, i)[0] < 100)
					{
						a[j][i] -= 1000000;
					}
					b[j][i] = ind;
				}
			}
		}
		double mini = 1000000000.0;
		int r = -1;
		int index[6000];

		for (int i = 0; i < temp_h; ++i)
		{
			if (a[i][temp_w - 1] <= mini)
			{
				mini = a[i][temp_w - 1];
				r = i;
			}
		}
		for (int i = temp_w - 1; i >= 1; --i)
		{
			index[i] = r;
			r = b[index[i]][i];

		}
		index[0] = r;

		for (int i = 0; i < temp_w; ++i)
		{
			w_seam_lines[w_count][i] = index[i];
		}
		w_count += 1;

		temp_h -= 1;

		for (int i = 0; i < temp_w; ++i) //update image
		{
			if (index[i] == 0)
			{
				this->img.at<Vec3b>(0, i)[0] = (this->img.at<Vec3b>(0, i)[0] + this->img.at<Vec3b>(1, i)[0]) / 2;
				this->img.at<Vec3b>(0, i)[1] = (this->img.at<Vec3b>(0, i)[1] + this->img.at<Vec3b>(1, i)[1]) / 2;
				this->img.at<Vec3b>(0, i)[2] = (this->img.at<Vec3b>(0, i)[2] + this->img.at<Vec3b>(1, i)[2]) / 2;
				for (int j = 1; j < temp_h; ++j)
				{
					this->img.at<Vec3b>(j, i)[0] = this->img.at<Vec3b>(j + 1, i)[0];
					this->img.at<Vec3b>(j, i)[1] = this->img.at<Vec3b>(j + 1, i)[1];
					this->img.at<Vec3b>(j, i)[2] = this->img.at<Vec3b>(j + 1, i)[2];
				}
			}
			else if (index[i] == temp_h)
			{
				this->img.at<Vec3b>(index[i] - 1, i)[0] = (this->img.at<Vec3b>(index[i] - 1, i)[0] + this->img.at<Vec3b>(index[i], i)[0]) / 2;
				this->img.at<Vec3b>(index[i] - 1, i)[1] = (this->img.at<Vec3b>(index[i] - 1, i)[1] + this->img.at<Vec3b>(index[i], i)[1]) / 2;
				this->img.at<Vec3b>(index[i] - 1, i)[2] = (this->img.at<Vec3b>(index[i] - 1, i)[2] + this->img.at<Vec3b>(index[i], i)[2]) / 2;
			}
			else
			{
				this->img.at<Vec3b>(index[i] - 1, i)[0] = (this->img.at<Vec3b>(index[i] - 1, i)[0] + this->img.at<Vec3b>(index[i], i)[0]) / 2;
				this->img.at<Vec3b>(index[i] - 1, i)[1] = (this->img.at<Vec3b>(index[i] - 1, i)[1] + this->img.at<Vec3b>(index[i], i)[1]) / 2;
				this->img.at<Vec3b>(index[i] - 1, i)[2] = (this->img.at<Vec3b>(index[i] - 1, i)[2] + this->img.at<Vec3b>(index[i], i)[2]) / 2;
				this->img.at<Vec3b>(index[i], i)[0] = (this->img.at<Vec3b>(index[i] + 1, i)[0] + this->img.at<Vec3b>(index[i], i)[0]) / 2;
				this->img.at<Vec3b>(index[i], i)[1] = (this->img.at<Vec3b>(index[i] + 1, i)[1] + this->img.at<Vec3b>(index[i], i)[1]) / 2;
				this->img.at<Vec3b>(index[i], i)[2] = (this->img.at<Vec3b>(index[i] + 1, i)[2] + this->img.at<Vec3b>(index[i], i)[2]) / 2;

				for (int j = index[i] + 1; j < temp_h; ++j)
				{
					this->img.at<Vec3b>(j, i)[0] = this->img.at<Vec3b>(j + 1, i)[0];
					this->img.at<Vec3b>(j, i)[1] = this->img.at<Vec3b>(j + 1, i)[1];
					this->img.at<Vec3b>(j, i)[2] = this->img.at<Vec3b>(j + 1, i)[2];
					if (del_obj == 1 && del_img.at<Vec3b>(j, i)[0] == 0)
					{
						this->del_img.at<Vec3b>(j, i)[0] = this->del_img.at<Vec3b>(j + 1, i)[0];
						this->del_img.at<Vec3b>(j, i)[1] = this->del_img.at<Vec3b>(j + 1, i)[1];
						this->del_img.at<Vec3b>(j, i)[2] = this->del_img.at<Vec3b>(j + 1, i)[2];
					}
				}
			}
		}

		for (int i = 0; i < temp_w; ++i) //update energy image
		{
			if (index[i] == 0)
			{
				energy_graph[0][i] = calculate_energy(0, i);
				for (int j = 1; j < temp_h; ++j)
				{
					energy_graph[j][i] = energy_graph[j + 1][i];
				}
			}
			else if (index[i] == temp_h)
			{
				energy_graph[index[i] - 1][i] = calculate_energy(index[i] - 1, i);
			}
			else
			{
				energy_graph[index[i]-1][i] = calculate_energy(index[i] - 1, i);
				energy_graph[index[i]][i] = calculate_energy(index[i], i);
				for (int j = index[i] + 1; j < temp_h; ++j)
				{
					energy_graph[j][i] = energy_graph[j + 1][i];
				}
			}
		}

		return;
	}
}

This snippet took 0.04 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).