out_img.width = img.width; out_img.height = img.height; qtView_Bridge::getInst("")->LogMessage(boost::format("%s: %s.") % __FUNCTION__ % (boost::format("OK! start YUV422TORAW8-> width:%d——heighht:%d") % (int)img.width%img.height)); out_img.type = qtRAW_8_MONO; out_img.vcm_pos = img.vcm_pos; out_img.byte_size = out_img.width * out_img.height * sizeof(unsigned char); out_img.data = new unsigned char[out_img.byte_size]; unsigned char* in_data = (unsigned char*)img.data; unsigned char* out_data = (unsigned char*)out_img.data; int i, j, k; int y, u, v; int r, g, b; for (i = 0, k = 0; i < in_img.height; i++) { for (j = 0; j < in_img.width; j += 2, k += 2) { y = in_data[k]; u = in_data[k + 1] - 128; v = in_data[k + 3] - 128; r = y + 1.402 * v; g = y - 0.34414 * u - 0.71414 * v; b = y + 1.772 * u; r = (r < 0) ? 0 : ((r > 255) ? 255 : r); g = (g < 0) ? 0 : ((g > 255) ? 255 : g); b = (b < 0) ? 0 : ((b > 255) ? 255 : b); out_data[i * in_img.width + j] = (unsigned char)((r + g + b) / 3); y = in_data[k + 2]; r = y + 1.402 * v; g = y - 0.34414 * u - 0.71414 * v; b = y + 1.772 * u; r = (r < 0) ? 0 : ((r > 255) ? 255 : r); g = (g < 0) ? 0 : ((g > 255) ? 255 : g); b = (b < 0) ? 0 : ((b > 255) ? 255 : b); out_data[i * in_img.width + j + 1] = (unsigned char)((r + g + b) / 3); } }这段代码哪里会导致代码出问题
时间: 2024-01-06 12:06:29 浏览: 165
_-1784656341_IMG_20190819_074341_1566171822000_xg.jpg
这段代码的问题可能出现在以下几个地方:
1. 使用了未定义的变量in_img而不是img,应该修改为img。
2. 在for循环中,应该使用out_img.height而不是in_img.height。
3. 在计算out_data索引时,应该使用out_img.width而不是in_img.width。
4. 在计算r、g、b时,应该先将u、v除以256再进行计算。
修正后的代码如下:
```
out_img.width = img.width;
out_img.height = img.height;
qtView_Bridge::getInst("")->LogMessage(boost::format("%s: %s.") % __FUNCTION__ % (boost::format("OK! start YUV422TORAW8-> width:%d——heighht:%d") % (int)img.width%img.height));
out_img.type = qtRAW_8_MONO;
out_img.vcm_pos = img.vcm_pos;
out_img.byte_size = out_img.width * out_img.height * sizeof(unsigned char);
out_img.data = new unsigned char[out_img.byte_size];
unsigned char* in_data = (unsigned char*)img.data;
unsigned char* out_data = (unsigned char*)out_img.data;
int i, j, k;
int y, u, v;
int r, g, b;
for (i = 0, k = 0; i < out_img.height; i++) {
for (j = 0; j < out_img.width; j += 2, k += 4) {
y = in_data[k];
u = in_data[k + 1] - 128;
v = in_data[k + 3] - 128;
r = y + 1.402 * (v/256.0);
g = y - 0.34414 * (u/256.0) - 0.71414 * (v/256.0);
b = y + 1.772 * (u/256.0);
r = (r < 0) ? 0 : ((r > 255) ? 255 : r);
g = (g < 0) ? 0 : ((g > 255) ? 255 : g);
b = (b < 0) ? 0 : ((b > 255) ? 255 : b);
out_data[i * out_img.width + j] = (unsigned char)((r + g + b) / 3);
y = in_data[k + 2];
r = y + 1.402 * (v/256.0);
g = y - 0.34414 * (u/256.0) - 0.71414 * (v/256.0);
b = y + 1.772 * (u/256.0);
r = (r < 0) ? 0 : ((r > 255) ? 255 : r);
g = (g < 0) ? 0 : ((g > 255) ? 255 : g);
b = (b < 0) ? 0 : ((b > 255) ? 255 : b);
out_data[i * out_img.width + j + 1] = (unsigned char)((r + g + b) / 3);
}
}
```
阅读全文