OpenCv,Android:无法编辑转换为 HLS 色彩空间的相机帧像素
OpenCv, Android: cannot edit pixels of camera frame converted to HLS colorspace
我有一个功能可以接收相机帧并对其进行 contrast/brightness 调整。当我有...
void applyContrastBrightnessToFrame(Mat &frame, float contrast, int brightness)
{
for (int i = 0; i < frame.rows; i++) {
uchar *basePixel = frame.ptr(i);
for (int j = 0; j != frame.cols * frame.channels(); j += frame.channels()) {
int channelsToBlend = min(3, frame.channels()); //never adjust alpha channel
for (int c = 0; c < channelsToBlend; c++) {
basePixel[j + c] = saturate_cast<uchar>(basePixel[j + c] * contrast + brightness);
}
}
}
}
效果很好。
但是当我将图像转换为 HLS 以便我可以在不破坏饱和度的情况下进行这些调整时,像素操作失败...
void applyContrastBrightnessToFrame(Mat &frame, float contrast, int brightness)
{
cvtColor(frame, frame, CV_RGBA2RGB);
cvtColor(frame, frame, CV_RGB2HLS);
assert(frame.channels() == 3);
for (int i = 0; i < frame.rows; i++) {
uchar *basePixel = frame.ptr(i);
for (int j = 0; j != frame.cols * frame.channels(); j += frame.channels()) {
int lumaChannel = 1;
//all pixel manipulations fail....
basePixel[j + lumaChannel] = 0; //setting to a constant
saturate_cast<uchar>(basePixel[j + lumaChannel] + brightness); //adjusting
}
}
cvtColor(frame, frame, CV_HLS2RGB);
cvtColor(frame, frame, CV_BGR2RGBA);
assert(frame.channels() == 4);
}
这是我所知道的:转换成功。当我从相机捕获图像并通过相同的函数 运行 时,像素操作成功 - 这特别奇怪,因为帧和捕获图像的处理是相同的。
可能出了什么问题?
我看到您正在尝试按像素更改 brightness/contrast 帧。
因此,您可以先拆分 HLS 通道,执行操作并将它们合并回来,而不是遍历帧的所有通道的每个像素。
void applyContrastBrightnessToFrame(Mat &frame, float contrast, int
brightness)
{
cvtColor(frame, frame, CV_RGBA2RGB);
cvtColor(frame, frame, CV_RGB2HLS);
vector<Mat> hlsChannels(3);
split(frame, hlsChannels);
hlsChannels[1] += brightness; //adding brightness to channel 2(lightness channel)
merge(hlschannels, frame);
cvtColor(frame, frame, CV_HLS2RGB);
cvtColor(frame, frame, CV_BGR2RGBA);
}
您也可以尝试单独遍历亮度通道中的像素。
希望对您有所帮助!
我有一个功能可以接收相机帧并对其进行 contrast/brightness 调整。当我有...
void applyContrastBrightnessToFrame(Mat &frame, float contrast, int brightness)
{
for (int i = 0; i < frame.rows; i++) {
uchar *basePixel = frame.ptr(i);
for (int j = 0; j != frame.cols * frame.channels(); j += frame.channels()) {
int channelsToBlend = min(3, frame.channels()); //never adjust alpha channel
for (int c = 0; c < channelsToBlend; c++) {
basePixel[j + c] = saturate_cast<uchar>(basePixel[j + c] * contrast + brightness);
}
}
}
}
效果很好。
但是当我将图像转换为 HLS 以便我可以在不破坏饱和度的情况下进行这些调整时,像素操作失败...
void applyContrastBrightnessToFrame(Mat &frame, float contrast, int brightness)
{
cvtColor(frame, frame, CV_RGBA2RGB);
cvtColor(frame, frame, CV_RGB2HLS);
assert(frame.channels() == 3);
for (int i = 0; i < frame.rows; i++) {
uchar *basePixel = frame.ptr(i);
for (int j = 0; j != frame.cols * frame.channels(); j += frame.channels()) {
int lumaChannel = 1;
//all pixel manipulations fail....
basePixel[j + lumaChannel] = 0; //setting to a constant
saturate_cast<uchar>(basePixel[j + lumaChannel] + brightness); //adjusting
}
}
cvtColor(frame, frame, CV_HLS2RGB);
cvtColor(frame, frame, CV_BGR2RGBA);
assert(frame.channels() == 4);
}
这是我所知道的:转换成功。当我从相机捕获图像并通过相同的函数 运行 时,像素操作成功 - 这特别奇怪,因为帧和捕获图像的处理是相同的。
可能出了什么问题?
我看到您正在尝试按像素更改 brightness/contrast 帧。
因此,您可以先拆分 HLS 通道,执行操作并将它们合并回来,而不是遍历帧的所有通道的每个像素。
void applyContrastBrightnessToFrame(Mat &frame, float contrast, int
brightness)
{
cvtColor(frame, frame, CV_RGBA2RGB);
cvtColor(frame, frame, CV_RGB2HLS);
vector<Mat> hlsChannels(3);
split(frame, hlsChannels);
hlsChannels[1] += brightness; //adding brightness to channel 2(lightness channel)
merge(hlschannels, frame);
cvtColor(frame, frame, CV_HLS2RGB);
cvtColor(frame, frame, CV_BGR2RGBA);
}
您也可以尝试单独遍历亮度通道中的像素。
希望对您有所帮助!