1

我正在制作一个应用程序来通过 opencv2 和来自 Internet 的一些转换代码来编辑图像的 HSL 颜色空间。

我想原始图像的色彩空间是 RGB,所以这是我的想法:

  1. 将 UIImage 转换为 cvMat
  2. 将色彩空间从 BGR 转换为 HLS。
  3. 循环遍历所有像素点,得到对应的 HLS 值。
  4. 自定义算法。
  5. 将 HLS 值更改重写为 cvMat
  6. 将 cvMat 转换为 UIImage

这是我的代码:

UIImage 和 cvMat 之间的转换

参考:https ://stackoverflow.com/a/10254561/1677041

#import <UIKit/UIKit.h>
#import <opencv2/core/core.hpp>

UIImage *UIImageFromCVMat(cv ::Mat cvMat)
{
    NSData *data = [NSData dataWithBytes:cvMat.data length:cvMat.elemSize() * cvMat.total()];

    CGColorSpaceRef colorSpace;
    CGBitmapInfo bitmapInfo;

    if (cvMat.elemSize() == 1) {
        colorSpace = CGColorSpaceCreateDeviceGray();
        bitmapInfo = kCGImageAlphaNone | kCGBitmapByteOrderDefault;
    } else {
        colorSpace = CGColorSpaceCreateDeviceRGB();
#if 0
        // OpenCV defaults to either BGR or ABGR. In CoreGraphics land,
        // this means using the "32Little" byte order, and potentially
        // skipping the first pixel. These may need to be adjusted if the
        // input matrix uses a different pixel format.
        bitmapInfo = kCGBitmapByteOrder32Little | (
            cvMat.elemSize() == 3? kCGImageAlphaNone : kCGImageAlphaNoneSkipFirst
        );
#else
        bitmapInfo = kCGImageAlphaNone | kCGBitmapByteOrderDefault;
#endif
    }

    CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);

    // Creating CGImage from cv::Mat
    CGImageRef imageRef = CGImageCreate(
        cvMat.cols,                 // width
        cvMat.rows,                 // height
        8,                          // bits per component
        8 * cvMat.elemSize(),       // bits per pixel
        cvMat.step[0],              // bytesPerRow
        colorSpace,                 // colorspace
        bitmapInfo,                 // bitmap info
        provider,                   // CGDataProviderRef
        NULL,                       // decode
        false,                      // should interpolate
        kCGRenderingIntentDefault   // intent
    );

    // Getting UIImage from CGImage
    UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpace);

    return finalImage;
}

cv::Mat cvMatWithImage(UIImage *image)
{
    CGColorSpaceRef colorSpace = CGImageGetColorSpace(image.CGImage);
    size_t numberOfComponents = CGColorSpaceGetNumberOfComponents(colorSpace);
    CGFloat cols = image.size.width;
    CGFloat rows = image.size.height;

    cv::Mat cvMat(rows, cols, CV_8UC4);  // 8 bits per component, 4 channels
    CGBitmapInfo bitmapInfo = kCGImageAlphaNoneSkipLast | kCGBitmapByteOrderDefault;

    // check whether the UIImage is greyscale already
    if (numberOfComponents == 1) {
        cvMat = cv::Mat(rows, cols, CV_8UC1);  // 8 bits per component, 1 channels
        bitmapInfo = kCGImageAlphaNone | kCGBitmapByteOrderDefault;
    }

    CGContextRef contextRef = CGBitmapContextCreate(
        cvMat.data,         // Pointer to backing data
        cols,               // Width of bitmap
        rows,               // Height of bitmap
        8,                  // Bits per component
        cvMat.step[0],      // Bytes per row
        colorSpace,         // Colorspace
        bitmapInfo          // Bitmap info flags
    );

    CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), image.CGImage);
    CGContextRelease(contextRef);

    return cvMat;
}

我单独测试了这两个功能并确认它们有效。

关于转换的核心操作:

/// Generate a new image based on specified HSL value changes.
/// @param h_delta h value in [-360, 360]
/// @param s_delta s value in [-100, 100]
/// @param l_delta l value in [-100, 100]
- (void)adjustImageWithH:(CGFloat)h_delta S:(CGFloat)s_delta L:(CGFloat)l_delta completion:(void (^)(UIImage *resultImage))completion
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        Mat original = cvMatWithImage(self.originalImage);
        Mat image;

        cvtColor(original, image, COLOR_BGR2HLS);
        // https://docs.opencv.org/2.4/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html#the-efficient-way

        // accept only char type matrices
        CV_Assert(image.depth() == CV_8U);

        int channels = image.channels();

        int nRows = image.rows;
        int nCols = image.cols * channels;

        int y, x;

        for (y = 0; y < nRows; ++y) {
            for (x = 0; x < nCols; ++x) {
                // https://answers.opencv.org/question/30547/need-to-know-the-hsv-value/
                // https://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html?#cvtcolor
                Vec3b hls = original.at<Vec3b>(y, x);
                uchar h = hls.val[0], l = hls.val[1], s = hls.val[2];

//              h = MAX(0, MIN(360, h + h_delta));
//              s = MAX(0, MIN(100, s + s_delta));
//              l = MAX(0, MIN(100, l + l_delta));

                 printf("(%02d, %02d):\tHSL(%d, %d, %d)\n", x, y, h, s, l); // <= Label 1

                 original.at<Vec3b>(y, x)[0] = h;
                 original.at<Vec3b>(y, x)[1] = l;
                 original.at<Vec3b>(y, x)[2] = s;
            }
        }

        cvtColor(image, image, COLOR_HLS2BGR);
        UIImage *resultImage = UIImageFromCVMat(image);

        dispatch_async(dispatch_get_main_queue(), ^ {
            if (completion) {
                completion(resultImage);
            }
        });
    });
}

问题是:

  1. 为什么 HLS 值超出我的预期范围?它显示为 [0, 255] 像 RGB 范围,是 cvtColor 错误使用吗?
  2. 我应该在两个 for 循环中使用 Vec3b 吗?还是 Vec3i?
  3. 我的想法上面有什么问题吗?

更新

Vec3b hls = original.at<Vec3b>(y, x);
uchar h = hls.val[0], l = hls.val[1], s = hls.val[2];

// Remap the hls value range to human-readable range (0~360, 0~1.0, 0~1.0).
// https://docs.opencv.org/master/de/d25/imgproc_color_conversions.html
float fh, fl, fs;
fh = h * 2.0;
fl = l / 255.0;
fs = s / 255.0;

fh = MAX(0, MIN(360, fh + h_delta));
fl = MAX(0, MIN(1, fl + l_delta / 100));
fs = MAX(0, MIN(1, fs + s_delta / 100));

// Convert them back
fh /= 2.0;
fl *= 255.0;
fs *= 255.0;

printf("(%02d, %02d):\tHSL(%d, %d, %d)\tHSL2(%.4f, %.4f, %.4f)\n", x, y, h, s, l, fh, fs, fl);

original.at<Vec3b>(y, x)[0] = short(fh);
original.at<Vec3b>(y, x)[1] = short(fl);
original.at<Vec3b>(y, x)[2] = short(fs);
4

1 回答 1

0

1)看看这个,特别是RGB-> HLS的部分。当源图像为 8 位时,它将从 0 到 255,但如果您使用浮点图像,它可能具有不同的值。

8 位图像:V←255⋅V、S←255⋅S、H←H/2(适应 0 到 255)

V 应该是 L,文档中有错字

您可以将 RGB/BGR 图像转换为浮点图像,然后您将获得完整的值。即S和L是从0到1,H是0-360。

但是您必须小心将其转换回来。

2) Vec3b 是无符号 8 位图像 (CV_8U) 而 Vec3i 是整数 (CV_32S)。知道这一点,这取决于您的图像是什么类型。正如你所说,它从 0 到 255 应该是无符号的 8 位,你应该使用 Vec3b。如果您使用另一个,它将获得每像素 32 位,并使用此大小来计算像素数组中的位置......所以它可能会给出诸如超出范围或分割错误或随机问题之类的东西。

如果您有任何问题,请随时发表评论

于 2019-10-10T08:39:34.447 回答