예제 #1
0
void
generate_image(png::image< pixel >& image)
{
    typedef png::pixel_traits< pixel > traits;
    size_t colors = 1 << traits::get_bit_depth();
    size_t size = colors / 2; 
    image.resize(size, size);

    png::palette palette(colors);
    for (size_t c = 0; c < colors; ++c)
    {
        palette[c] = png::color(c * 255 / colors,
                                (colors - c - 1) * 255 / colors,
                                c * 255 / colors);
    }
    image.set_palette(palette);
    for (size_t j = 0; j < image.get_height(); ++j)
    {
        for (size_t i = 0; i < image.get_width(); ++i)
        {
            image.set_pixel(i, j, i + j);
        }
    }
}
void makeSegmentBoundaryImage(const png::image<png::rgb_pixel>& inputImage,
							  const png::image<png::gray_pixel_16>& segmentImage,
							  std::vector< std::vector<int> >& boundaryLabels,
							  png::image<png::rgb_pixel>& segmentBoundaryImage)
{
	int width = static_cast<int>(inputImage.get_width());
	int height = static_cast<int>(inputImage.get_height());
	int boundaryTotal = static_cast<int>(boundaryLabels.size());

	segmentBoundaryImage.resize(width, height);
	for (int y = 0; y < height; ++y) {
		for (int x = 0; x < width; ++x) {
			segmentBoundaryImage.set_pixel(x, y, inputImage.get_pixel(x, y));
		}
	}

	int boundaryWidth = 2;
	for (int y = 0; y < height - 1; ++y) {
		for (int x = 0; x < width - 1; ++x) {
			int pixelLabelIndex = segmentImage.get_pixel(x, y);

			if (segmentImage.get_pixel(x + 1, y) != pixelLabelIndex) {
				for (int w = 0; w < boundaryWidth - 1; ++w) {
					if (x - w >= 0) segmentBoundaryImage.set_pixel(x - w, y, png::rgb_pixel(128, 128, 128));
				}
				for (int w = 1; w < boundaryWidth; ++w) {
					if (x + w < width) segmentBoundaryImage.set_pixel(x + w, y, png::rgb_pixel(128, 128, 128));
				}
			}
			if (segmentImage.get_pixel(x, y + 1) != pixelLabelIndex) {
				for (int w = 0; w < boundaryWidth - 1; ++w) {
					if (y - w >= 0) segmentBoundaryImage.set_pixel(x, y - w, png::rgb_pixel(128, 128, 128));
				}
				for (int w = 1; w < boundaryWidth; ++w) {
					if (y + w < height) segmentBoundaryImage.set_pixel(x, y + w, png::rgb_pixel(128, 128, 128));
				}
			}
		}
	}

	boundaryWidth = 7;
	for (int y = 0; y < height - 1; ++y) {
		for (int x = 0; x < width - 1; ++x) {
			int pixelLabelIndex = segmentImage.get_pixel(x, y);

			if (segmentImage.get_pixel(x + 1, y) != pixelLabelIndex) {
				png::rgb_pixel negativeSideColor, positiveSideColor;
				int pixelBoundaryIndex = -1;
				for (int boundaryIndex = 0; boundaryIndex < boundaryTotal; ++boundaryIndex) {
					if ((boundaryLabels[boundaryIndex][0] == pixelLabelIndex && boundaryLabels[boundaryIndex][1] == segmentImage.get_pixel(x + 1, y))
						|| (boundaryLabels[boundaryIndex][0] == segmentImage.get_pixel(x + 1, y) && boundaryLabels[boundaryIndex][1] == pixelLabelIndex))
					{
						pixelBoundaryIndex = boundaryIndex;
						break;
					}
				}
				if (boundaryLabels[pixelBoundaryIndex][2] == 3) continue;
				else if (boundaryLabels[pixelBoundaryIndex][2] == 2) {
					negativeSideColor.red = 0;  negativeSideColor.green = 225;  negativeSideColor.blue = 0;
					positiveSideColor.red = 0;  positiveSideColor.green = 225;  positiveSideColor.blue = 0;
				} else if (pixelLabelIndex == boundaryLabels[pixelBoundaryIndex][boundaryLabels[pixelBoundaryIndex][2]]) {
					negativeSideColor.red = 225;  negativeSideColor.green = 0;  negativeSideColor.blue = 0;
					positiveSideColor.red = 0;  positiveSideColor.green = 0;  positiveSideColor.blue = 225;
				} else {
					negativeSideColor.red = 0;  negativeSideColor.green = 0;  negativeSideColor.blue = 225;
					positiveSideColor.red = 225;  positiveSideColor.green = 0;  positiveSideColor.blue = 0;
				}

				for (int w = 0; w < boundaryWidth - 1; ++w) {
					if (x - w >= 0) segmentBoundaryImage.set_pixel(x - w, y, negativeSideColor);
				}
				for (int w = 1; w < boundaryWidth; ++w) {
					if (x + w < width) segmentBoundaryImage.set_pixel(x + w, y, positiveSideColor);
				}
			}
			if (segmentImage.get_pixel(x, y + 1) != pixelLabelIndex) {
				png::rgb_pixel negativeSideColor, positiveSideColor;
				int pixelBoundaryIndex = -1;
				for (int boundaryIndex = 0; boundaryIndex < boundaryTotal; ++boundaryIndex) {
					if ((boundaryLabels[boundaryIndex][0] == pixelLabelIndex && boundaryLabels[boundaryIndex][1] == segmentImage.get_pixel(x, y + 1))
						|| (boundaryLabels[boundaryIndex][0] == segmentImage.get_pixel(x, y + 1) && boundaryLabels[boundaryIndex][1] == pixelLabelIndex))
					{
						pixelBoundaryIndex = boundaryIndex;
						break;
					}
				}
				if (boundaryLabels[pixelBoundaryIndex][2] == 3) continue;
				else if (boundaryLabels[pixelBoundaryIndex][2] == 2) {
					negativeSideColor.red = 0;  negativeSideColor.green = 225;  negativeSideColor.blue = 0;
					positiveSideColor.red = 0;  positiveSideColor.green = 225;  positiveSideColor.blue = 0;
				} else if (pixelLabelIndex == boundaryLabels[pixelBoundaryIndex][boundaryLabels[pixelBoundaryIndex][2]]) {
					negativeSideColor.red = 225;  negativeSideColor.green = 0;  negativeSideColor.blue = 0;
					positiveSideColor.red = 0;  positiveSideColor.green = 0;  positiveSideColor.blue = 225;
				} else {
					negativeSideColor.red = 0;  negativeSideColor.green = 0;  negativeSideColor.blue = 225;
					positiveSideColor.red = 225;  positiveSideColor.green = 0;  positiveSideColor.blue = 0;
				}

				for (int w = 0; w < boundaryWidth - 1; ++w) {
					if (y - w >= 0) segmentBoundaryImage.set_pixel(x, y - w, negativeSideColor);
				}
				for (int w = 1; w < boundaryWidth; ++w) {
					if (y+ w < height) segmentBoundaryImage.set_pixel(x, y + w, positiveSideColor);
				}
			}
		}
	}
}