在我的应用程序中,我需要比较2张图像并突出显示颜色差异。例如,我正在将UIImage1与UIImage2进行比较,其中两个图像相同,只是UIImage2中有一个小方块。我需要用红色突出显示额外的正方形对象并在图像视图中显示,否则我想知道差异的x和y方向。
我已经尝试过以下代码。在这段代码中,我只能更改差异的alpha值。无法更改差异的颜色。
UIImage* bottomImage = [UIImage imageNamed:@"j1.jpg"];
UIImage* topImage = [UIImage imageNamed:@"j2.jpg"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:bottomImage];
UIImageView* subView = [[UIImageView alloc] initWithImage:topImage];
subView.alpha = 0.5; // Customize the opacity of the top image.
[imageView addSubview:subView];
UIGraphicsBeginImageContext(imageView.frame.size);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage* blendedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
_imageview.image = blendedImage;
图片1:
图片2:
差异图片:
但是我想要的是这个
我是OpenCV的新手,代码在python中。谁能帮助我将此代码更改为Objective-C?我尝试过,但仅收到错误消息。
https://stackoverflow.com/a/27500479/6859041
im = cv2.imread('c:\\diff.jpg')
im1 = cv2.imread('c:\\Edited.jpg')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im1, contours, -1, (0,255,0), 1)
cv2.imwrite("c:\\see_this.jpg", im1)
如何将此代码更改为目标C?
谢谢。
答案 0 :(得分:0)
您可以检查Facebook解决方案的代码以进行测试,称为Snapshot:https://github.com/uber/ios-snapshot-test-case/
查看类别文件夹。
- (UIImage *)fb_diffWithImage:(UIImage *)image
{
if (!image) {
return nil;
}
CGSize imageSize = CGSizeMake(MAX(self.size.width, image.size.width), MAX(self.size.height, image.size.height));
UIGraphicsBeginImageContextWithOptions(imageSize, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
CGContextSetAlpha(context, 0.5);
CGContextBeginTransparencyLayer(context, NULL);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
CGContextSetBlendMode(context, kCGBlendModeDifference);
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, self.size.width, self.size.height));
CGContextEndTransparencyLayer(context);
UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return returnImage;
}
答案 1 :(得分:0)
您可以直接查看图像的背景数据以获取任何像素的RGBA值,然后使用它们来比较或构建新图像。不过,在CPU上执行此操作非常昂贵。您可能会发现编写自定义GPU着色器的速度更快,该着色器可让jsut比较两个输入并根据它们是否相等来写入输出。
func pixel(in image: UIImage, at point: CGPoint) -> (UInt8, UInt8, UInt8, UInt8)? {
let width = Int(image.size.width)
let height = Int(image.size.height)
let x = Int(point.x)
let y = Int(point.y)
guard x < width && y < height else {
return nil
}
guard let cfData:CFData = image.cgImage?.dataProvider?.data, let pointer = CFDataGetBytePtr(cfData) else {
return nil
}
let bytesPerPixel = 4
let offset = (x + y * width) * bytesPerPixel
return (pointer[offset], pointer[offset + 1], pointer[offset + 2], pointer[offset + 3])
}
let image = UIImage(named: "t.png")!
if let (r,g,b,a) = pixel(in: image, at: CGPoint(x: 1, y:2)) {
print ("Red: \(r), Green: \(g), Blue: \(b), Alpha: \(a)")
}
答案 2 :(得分:0)
使用ImageMagick,这非常容易。
compare -fuzz 25% -metric rmse -lowlight-color transparent -highlight-color red img1.jpg img2.jpg diffimage.jpg
1562.23 (0.0238381)
您可以在Python魔杖中执行相同的操作,该魔杖调用ImageMagick并获得与上述相同的图像和值。 (归因于emcconville)
#!/bin/python3.7
from wand.image import Image
from wand.display import display
from wand.api import library
with Image(filename='img1.jpg') as bimg:
with Image(filename='img2.jpg') as fimg:
# We have to call the C method directly, and calculate the percentage.
library.MagickSetImageFuzz(bimg.wand, 0.25 * bimg.quantum_range)
bimg.artifacts['compare:highlight-color'] = 'red'
bimg.artifacts['compare:lowlight-color'] = 'transparent'
diff_img, diff_val = bimg.compare(fimg, 'root_mean_square')
print(diff_val)
with diff_img:
diff_img.save(filename='img1_img2_diff.jpg')