簡體   English   中英

如何從圖像創建蒙版

[英]How to create a mask from an image

我在NSbundle中有一張圖片。 下面是圖片

http://tinypic.com/view.php?pic=2093tvt&s=5

我想將此圖像用作遮罩圖像,並希望將其應用於任何圖像,以便其他圖像將具有與上述圖像相同的形狀。

所以這是一個重復的問題,但對我不起作用。

正如上面的問題,建議對圖像進行縮放,所以我也這樣做了。 我通過代碼將圖像更改為灰度

- (UIImage *) convertToGreyscale:(UIImage *)i {

int kRed = 1;
int kGreen = 2;
int kBlue = 4;

int colors = kGreen;
int m_width = i.size.width;
int m_height = i.size.height;

uint32_t *rgbImage = (uint32_t *) malloc(m_width * m_height * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rgbImage, m_width, m_height, 8, m_width * 4, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextSetShouldAntialias(context, NO);
CGContextDrawImage(context, CGRectMake(0, 0, m_width, m_height), [i CGImage]);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);

// now convert to grayscale
uint8_t *m_imageData = (uint8_t *) malloc(m_width * m_height);
for(int y = 0; y < m_height; y++) {
    for(int x = 0; x < m_width; x++) {
        uint32_t rgbPixel=rgbImage[y*m_width+x];
        uint32_t sum=0,count=0;
        if (colors & kRed) {sum += (rgbPixel>>24)&255; count++;}
        if (colors & kGreen) {sum += (rgbPixel>>16)&255; count++;}
        if (colors & kBlue) {sum += (rgbPixel>>8)&255; count++;}
        m_imageData[y*m_width+x]=sum/count;
    }
}
free(rgbImage);

// convert from a gray scale image back into a UIImage
uint8_t *result = (uint8_t *) calloc(m_width * m_height *sizeof(uint32_t), 1);

// process the image back to rgb
for(int i = 0; i < m_height * m_width; i++) {
    result[i*4]=0;
    int val=m_imageData[i];
    result[i*4+1]=val;
    result[i*4+2]=val;
    result[i*4+3]=val;
}

// create a UIImage
colorSpace = CGColorSpaceCreateDeviceRGB();
context = CGBitmapContextCreate(result, m_width, m_height, 8, m_width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
UIImage *resultUIImage = [UIImage imageWithCGImage:image];
CGImageRelease(image);

free(m_imageData);

// make sure the data will be released by giving it to an autoreleased NSData
[NSData dataWithBytesNoCopy:result length:m_width * m_height];

return resultUIImage;
}

下面是灰度圖像

鏈接1:

當我遮蓋圖像時,結果我得到的結果低於注釋中給出的鏈接。 我不能添加超過2個鏈接

鏈接2:

我想我沒有創建適當的蒙版圖像

我正在嘗試遮蓋圖片http://tinypic.com/view.php?pic=10di24m&s=5

有人可以告訴我制作遮罩圖像的方法嗎?

您所使用的方法Graysclae似乎存在問題,它使圓圈外的顏色變為黑色,但應將其設置為白色以正確掩蓋它。

這是我寫的圖像類別

UIImage + Mask.h

#import <UIKit/UIKit.h>

@interface UIImage (Mask)

- (UIImage*) maskImage:(UIImage *) image;
- (UIImage *)greyscaleImage;
@end

UIImage + Mask.m

@implementation UIImage (Mask)


- (UIImage*) maskImage:(UIImage *) image
{
    CGImageRef imageReference = image.CGImage;
    CGImageRef maskReference = self.CGImage;

    CGImageRef imageMask = CGImageMaskCreate(CGImageGetWidth(maskReference),
                                             CGImageGetHeight(maskReference),
                                             CGImageGetBitsPerComponent(maskReference),
                                             CGImageGetBitsPerPixel(maskReference),
                                             CGImageGetBytesPerRow(maskReference),
                                             CGImageGetDataProvider(maskReference),
                                             NULL, // Decode is null
                                             YES // Should interpolate
                                             );

    CGImageRef maskedReference = CGImageCreateWithMask(imageReference, imageMask);
    CGImageRelease(imageMask);

    UIImage *maskedImage = [UIImage imageWithCGImage:maskedReference];
    CGImageRelease(maskedReference);

    return maskedImage;
}

- (UIImage *)greyscaleImage {

    CGImageRef imgRef = self.CGImage;

    // Get the image width
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    // Create a Grayscale colour space
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();

    /*
     * Create the context - based on orientation
     */
    CGContextRef context = nil;

    // If the orientation isn't UIImageOrientationUp then we'll need to rotate
    UIImageOrientation orient = self.imageOrientation;

    switch (orient) {
        case UIImageOrientationUp:
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
        case UIImageOrientationUpMirrored:

            context = CGBitmapContextCreate(NULL, width, height, 8, 0, colorSpace, kCGImageAlphaNone);
            break;

        default:
            context = CGBitmapContextCreate(NULL, height, width, 8, 0, colorSpace, kCGImageAlphaNone);
            break;
    }

    CGColorSpaceRelease(colorSpace);

    /*
     * Create a context translation/rotation based on the orientation
     */
    switch(orient) {
        case UIImageOrientationUpMirrored:
            // 2 = 0th row is at the top, and 0th column is on the right - Flip Horizontal
            CGContextConcatCTM(context, CGAffineTransformMake(-1.0, 0.0, 0.0, 1.0, width, 0.0));
            break;

        case UIImageOrientationDown:
            // 3 = 0th row is at the bottom, and 0th column is on the right - Rotate 180 degrees
            CGContextConcatCTM(context, CGAffineTransformMake(-1.0, 0.0, 0.0, -1.0, width, height));
            break;

        case UIImageOrientationDownMirrored:
            // 4 = 0th row is at the bottom, and 0th column is on the left - Flip Vertical
            CGContextConcatCTM(context, CGAffineTransformMake(1.0, 0.0, 0, -1.0, 0.0, height));
            break;

        case UIImageOrientationLeftMirrored:
            // 5 = 0th row is on the left, and 0th column is the top - Rotate -90 degrees and Flip Vertical
            CGContextConcatCTM(context, CGAffineTransformMake(0.0, -1.0, -1.0, 0.0, height, width));
            break;

        case UIImageOrientationRight:
            // 6 = 0th row is on the right, and 0th column is the top - Rotate 90 degrees
            CGContextConcatCTM(context, CGAffineTransformMake(0.0, -1.0, 1.0, 0.0, 0.0, width));
            break;

        case UIImageOrientationRightMirrored:
            // 7 = 0th row is on the right, and 0th column is the bottom - Rotate 90 degrees and Flip Vertical
            CGContextConcatCTM(context, CGAffineTransformMake(0.0, 1.0, 1.0, 0.0, 0.0, 0.0));
            break;

        case UIImageOrientationLeft:
            // 8 = 0th row is on the left, and 0th column is the bottom - Rotate -90 degrees
            CGContextConcatCTM(context, CGAffineTransformMake(0.0, 1.0, -1.0, 0.0, height, 0.0));
            break;

        case UIImageOrientationUp:
            // Nothing to do here
            break;
    }

    /*
     * Create the new image
     */
    CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
    CGContextFillRect(context, CGRectMake(0, 0, width, height));
    //CGContextFillEllipseInRect(context, );
    // Draw the image to the context
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), imgRef);

    // Get a UIImage from the context
    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *imageCopy =  [UIImage imageWithCGImage:imageRef];

    // Release
    CGImageRelease(imageRef);
    CGContextRelease(context);

    return imageCopy;
}

@end

這是用法

UIImage *maskImage = [UIImage imageNamed:@"maskimage"];  // in your case the red dot image
UIImage *image = [UIImage imageNamed:@"image"];   // image to mask
maskImage = [maskImage greyscaleImage];
image = [maskImage maskImage:image];
self.imageView.image = image;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM