[英]C++ Create and Save OpenCV 16bit image pixelwise
我想通過手動設置像素值以產生噪聲,用OpenCV創建一個16位的PNG圖像。 我嘗試了以下方法:
cv::Mat img16(WINDOW_Y, WINDOW_X, CV_16UC3); // create 16 bit mat
float val, new_value;
float noise = 3.1343; // actually randomly generated by randomizer in (0..255)
for (int x = 0; x < WINDOW_X; x++) {
for (int y = 0; y < WINDOW_Y; y++) {
for (int i = 0; i < 3; i++) {
// get value from another CV_8UC3-mat
val = img.at<cv::Vec3b>(y,x)[i];
// add up noise and scale to 16bit
new_value = (val + noise)*255;
// avoid overflow at 2^16-1
if (new_value > 65535) {
new_value = 65535;
}
// set the value to 16bit mat
img16.at<cv::Vec3s>(y,x)[i] = (short) new_value;
}
}
}
// write to PNG file, since PNG supports 16 bit
cv::imwrite(file + ".png", img16);
我得到的輸出似乎是16位:
$ identify output.png
output.png PNG 128x128 128x128+0+0 16-bit PseudoClass 65536c
但是它顯示出單調灰色(#808080),而我希望圖像是異類的。 它出什么問題了?
每當您發現自己在opencv中編寫每個像素的循環時,請-堅持嘗試並嘗試找到內置的內容。
Mat ocv=Mat(200,200,CV_16UC3); // as you can only save 16bit *unsigned* as png or tif
theRNG().fill(ocv,RNG::UNIFORM,0,65535);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.