static uint8_t* bgra_to_rgb(const Eigen::MapRMatVec4Ub& bgra) {
     unsigned size = sizeof(Eigen::Vector3Ub) * bgra.cols() * bgra.rows();
     uint8_t* rgb = new uint8_t[size], *rgb_end = rgb + size - 1;
     for(int i = 0; i < bgra.rows(); ++i) {
         for(int j = 0; j < bgra.cols(); ++j) {
             Eigen::Vector3Ub* n = &(((Eigen::Vector3Ub*)rgb)[i * bgra.cols() + j]);
             assert(i * bgra.cols() + j < bgra.cols() * bgra.rows());
             assert((uint8_t*)n < rgb_end);
             *n = (Eigen::Vector3Ub() << bgra(i, j)(2), bgra(i, j)(1), bgra(i, j)(0)).finished();
             #ifndef NDEBUG
             void* m = (void*)&(rgb[3 * j + 3 * i * bgra.cols()]);
             //printf("Pointer %lu soll, aber %lu bei %d, %d\n", (uint64_t)n, (uint64_t)m, i, j);
             assert((void*)n == m);
             #endif
         }
     }
     return rgb;
 }
Esempio n. 2
0
// ".png" or ".jpg"
std::vector<unsigned char> compressImage(const cv::Mat & image, const std::string & format)
{
	std::vector<unsigned char> bytes;
	if(!image.empty())
	{
		if(image.type() == CV_32FC1)
		{
			//save in 8bits-4channel
			cv::Mat bgra(image.size(), CV_8UC4, image.data);
			cv::imencode(format, bgra, bytes);
		}
		else
		{
			cv::imencode(format, image, bytes);
		}
	}
	return bytes;
}