Пример #1
0
/**
 * Filters the given image using the QuadcopterColor.
 * The result is written to mapImage. White pixels mean in range,
 * black pixels mean out of range.
 *
 * @param image The raw image.
 * @param mapImage The resulting mapped image. (Output array)
 * @param hsvImage The raw image in hsv format. (Output array)
 */
cv::Mat Tracker::createColorMapImage(cv::Mat &image, cv::Mat &mapImage, cv::Mat &hsvImage)
{
	START_CLOCK(convertColorClock)

	// This ensures that the mapImage has a buffer.
	// Since the buffer is never freed during tracking, this only allocates memory once.
	mapImage.reserve(480);

	cv::cvtColor(image, hsvImage, CV_BGR2HSV);

	STOP_CLOCK(convertColorClock, "Converting colors took: ")
	START_CLOCK(maskImageClock)

	uint8_t * current, *end, *source;
	int minHue, maxHue, minSaturation, minValue;

	QuadcopterColor *color = (QuadcopterColor*) qc;

	minHue = color->getMinColor().val[0];
	maxHue = color->getMaxColor().val[0];
	minSaturation = color->getMinColor().val[1];
	minValue = color->getMinColor().val[2];

	end = mapImage.data + mapImage.size().width * mapImage.size().height;
	source = hsvImage.data;

	if (minHue < maxHue)
		for (current = mapImage.data; current < end; ++current, source += 3) {
			if (*source > maxHue || *source < minHue || *(source + 1) < minSaturation || *(source + 2) < minValue)
				*current = 0;
			else
				*current = 255;
		}
	else
		// Hue interval inverted here.
		for (current = mapImage.data; current < end; ++current, source += 3) {
			if ((*source > maxHue && *source < minHue) || *(source + 1) < minSaturation || *(source + 2) < minValue)
				*current = 0;
			else
				*current = 255;
		}

	STOP_CLOCK(maskImageClock, "Image masking took: ")

	return mapImage;
}