chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:06:04 +08:00
commit 86c9b1c39f
7743 changed files with 3316339 additions and 0 deletions
@@ -0,0 +1,100 @@
package org.opencv.test.imgcodecs;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfInt;
import org.opencv.imgproc.Imgproc;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgcodecs.Animation;
import org.opencv.test.OpenCVTestCase;
import org.opencv.test.OpenCVTestRunner;
import java.util.ArrayList;
import java.util.List;
public class ImgcodecsTest extends OpenCVTestCase {
public void testAnimation() {
if (!Imgcodecs.haveImageWriter("*.apng")) {
return;
}
Mat src = Imgcodecs.imread(OpenCVTestRunner.LENA_PATH, Imgcodecs.IMREAD_REDUCED_COLOR_4);
assertFalse(src.empty());
Mat rgb = new Mat();
Imgproc.cvtColor(src, rgb, Imgproc.COLOR_BGR2RGB);
Animation animation = new Animation();
List<Mat> frames = new ArrayList<>();
MatOfInt durations = new MatOfInt(100, 100);
frames.add(src);
frames.add(rgb);
animation.set_frames(frames);
animation.set_durations(durations);
String filename = OpenCVTestRunner.getTempFileName("png");
assertTrue(Imgcodecs.imwriteanimation(filename, animation));
Animation readAnimation = new Animation();
assertTrue(Imgcodecs.imreadanimation(filename, readAnimation));
List<Mat> readFrames = readAnimation.get_frames();
assertTrue(readFrames.size() == 2);
}
public void testImdecode() {
fail("Not yet implemented");
}
public void testImencodeStringMatListOfByte() {
MatOfByte buff = new MatOfByte();
assertEquals(0, buff.total());
assertTrue( Imgcodecs.imencode(".jpg", gray127, buff) );
assertFalse(0 == buff.total());
}
public void testImencodeStringMatListOfByteListOfInteger() {
MatOfInt params40 = new MatOfInt(Imgcodecs.IMWRITE_JPEG_QUALITY, 40);
MatOfInt params90 = new MatOfInt(Imgcodecs.IMWRITE_JPEG_QUALITY, 90);
/* or
MatOfInt params = new MatOfInt();
params.fromArray(Imgcodecs.IMWRITE_JPEG_QUALITY, 40);
*/
MatOfByte buff40 = new MatOfByte();
MatOfByte buff90 = new MatOfByte();
assertTrue( Imgcodecs.imencode(".jpg", rgbLena, buff40, params40) );
assertTrue( Imgcodecs.imencode(".jpg", rgbLena, buff90, params90) );
assertTrue(buff40.total() > 0);
assertTrue(buff40.total() < buff90.total());
}
public void testImreadString() {
dst = Imgcodecs.imread(OpenCVTestRunner.LENA_PATH);
assertFalse(dst.empty());
assertEquals(3, dst.channels());
assertTrue(512 == dst.cols());
assertTrue(512 == dst.rows());
}
public void testImreadStringInt() {
dst = Imgcodecs.imread(OpenCVTestRunner.LENA_PATH, Imgcodecs.IMREAD_GRAYSCALE);
assertFalse(dst.empty());
assertEquals(1, dst.channels());
assertTrue(512 == dst.cols());
assertTrue(512 == dst.rows());
}
public void testImwriteStringMat() {
fail("Not yet implemented");
}
public void testImwriteStringMatListOfInteger() {
fail("Not yet implemented");
}
}
@@ -0,0 +1,5 @@
{
"SourceMap" : {
"visionos" : "ios"
}
}
@@ -0,0 +1,32 @@
//
// MatConverters.h
//
// Created by Giles Payne on 2020/03/03.
//
#pragma once
#ifdef __cplusplus
#import "opencv2/core.hpp"
#else
#define CV_EXPORTS
#endif
#import "Mat.h"
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
CV_EXPORTS @interface MatConverters : NSObject
+(CGImageRef)convertMatToCGImageRef:(Mat*)mat CF_RETURNS_RETAINED;
+(Mat*)convertCGImageRefToMat:(CGImageRef)image;
+(Mat*)convertCGImageRefToMat:(CGImageRef)image alphaExist:(BOOL)alphaExist;
+(UIImage*)converMatToUIImage:(Mat*)mat;
+(Mat*)convertUIImageToMat:(UIImage*)image;
+(Mat*)convertUIImageToMat:(UIImage*)image alphaExist:(BOOL)alphaExist;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,40 @@
//
// MatConverters.mm
//
// Created by Giles Payne on 2020/03/03.
//
#import "MatConverters.h"
#import <opencv2/imgcodecs/ios.h>
@implementation MatConverters
+(CGImageRef)convertMatToCGImageRef:(Mat*)mat {
return MatToCGImage(mat.nativeRef);
}
+(Mat*)convertCGImageRefToMat:(CGImageRef)image {
return [MatConverters convertCGImageRefToMat:image alphaExist:NO];
}
+(Mat*)convertCGImageRefToMat:(CGImageRef)image alphaExist:(BOOL)alphaExist {
Mat* mat = [Mat new];
CGImageToMat(image, mat.nativeRef, (bool)alphaExist);
return mat;
}
+(UIImage*)converMatToUIImage:(Mat*)mat {
return MatToUIImage(mat.nativeRef);
}
+(Mat*)convertUIImageToMat:(UIImage*)image {
return [MatConverters convertUIImageToMat:image alphaExist:NO];
}
+(Mat*)convertUIImageToMat:(UIImage*)image alphaExist:(BOOL)alphaExist {
Mat* mat = [Mat new];
UIImageToMat(image, mat.nativeRef, (bool)alphaExist);
return mat;
}
@end
@@ -0,0 +1,27 @@
//
// MatQuickLook.h
//
// Created by Giles Payne on 2021/07/18.
//
#pragma once
#ifdef __cplusplus
#import "opencv2/core.hpp"
#else
#define CV_EXPORTS
#endif
#import "Mat.h"
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
CV_EXPORTS @interface MatQuickLook : NSObject
+ (id)matDebugQuickLookObject:(Mat*)mat;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,158 @@
//
// MatQuickLook.mm
//
// Created by Giles Payne on 2021/07/18.
//
#import "MatQuickLook.h"
#import "Rect2i.h"
#import "Core.h"
#import "Imgproc.h"
#import <opencv2/imgcodecs/ios.h>
#define SIZE 20
static UIFont* getCMU() {
return [UIFont fontWithName:@"CMU Serif" size:SIZE];
}
static UIFont* getBodoni72() {
return [UIFont fontWithName:@"Bodoni 72" size:SIZE];
}
static UIFont* getAnySerif() {
#if defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
if (@available(iOS 13.0, *)) {
return [UIFont fontWithDescriptor:[[UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleBody] fontDescriptorWithDesign:UIFontDescriptorSystemDesignSerif] size:SIZE];
} else {
return nil;
}
#else
return nil;
#endif
}
static UIFont* getSystemFont() {
return [UIFont systemFontOfSize:SIZE];
}
typedef UIFont* (*FontGetter)();
@implementation MatQuickLook
+ (NSString*)makeLabel:(BOOL)isIntType val:(NSNumber*)num {
if (isIntType) {
return [NSString stringWithFormat:@"%d", num.intValue];
} else {
int exponent = 1 + (int)log10(abs(num.doubleValue));
if (num.doubleValue == (double)num.intValue && num.doubleValue < 10000 && num.doubleValue > -10000) {
return [NSString stringWithFormat:@"%d", num.intValue];;
} else if (exponent <= 5 && exponent >= -1) {
return [NSString stringWithFormat:[NSString stringWithFormat:@"%%%d.%df", 6, MIN(5 - exponent, 4)], num.doubleValue];
} else {
return [[[NSString stringWithFormat:@"%.2e", num.doubleValue] stringByReplacingOccurrencesOfString:@"e+0" withString:@"e"] stringByReplacingOccurrencesOfString:@"e-0" withString:@"e-"];
}
}
}
+ (void)relativeLine:(UIBezierPath*)path relX:(CGFloat)x relY:(CGFloat)y {
CGPoint curr = path.currentPoint;
[path addLineToPoint:CGPointMake(curr.x + x, curr.y + y)];
}
+ (id)matDebugQuickLookObject:(Mat*)mat {
if ([mat dims] == 2 && [mat rows] <= 10 && [mat cols] <= 10 && [mat channels] == 1) {
FontGetter fontGetters[] = { getCMU, getBodoni72, getAnySerif, getSystemFont };
UIFont* font = nil;
for (int fontGetterIndex = 0; font==nil && fontGetterIndex < (sizeof(fontGetters)) / (sizeof(fontGetters[0])); fontGetterIndex++) {
font = fontGetters[fontGetterIndex]();
}
int elements = [mat rows] * [mat cols];
NSDictionary<NSAttributedStringKey,id>* textFontAttributes = @{ NSFontAttributeName: font, NSForegroundColorAttributeName: UIColor.blackColor };
NSMutableArray<NSNumber*>* rawData = [NSMutableArray new];
for (int dataIndex = 0; dataIndex < elements; dataIndex++) {
[rawData addObject:[NSNumber numberWithDouble:0]];
}
[mat get:0 col: 0 data: rawData];
BOOL isIntType = [mat depth] <= CV_32S;
NSMutableArray<NSString*>* labels = [NSMutableArray new];
NSMutableDictionary<NSString*, NSValue*>* boundingRects = [NSMutableDictionary dictionaryWithCapacity:elements];
int maxWidth = 0, maxHeight = 0;
for (NSNumber* number in rawData) {
NSString* label = [MatQuickLook makeLabel:isIntType val:number];
[labels addObject:label];
CGRect boundingRect = [label boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:textFontAttributes context:nil];
if (boundingRect.size.width > maxWidth) {
maxWidth = boundingRect.size.width;
}
if (boundingRect.size.height > maxHeight) {
maxHeight = boundingRect.size.height;
}
boundingRects[label] = [NSValue valueWithCGRect:boundingRect];
}
int rowGap = 6;
int colGap = 6;
int borderGap = 8;
int lineThickness = 3;
int lipWidth = 6;
int imageWidth = 2 * (borderGap + lipWidth) + maxWidth * [mat cols] + colGap * ([mat cols] - 1);
int imageHeight = 2 * (borderGap + lipWidth) + maxHeight * [mat rows] + rowGap * ([mat rows] - 1);
UIBezierPath* leftBracket = [UIBezierPath new];
[leftBracket moveToPoint:CGPointMake(borderGap, borderGap)];
[MatQuickLook relativeLine:leftBracket relX:0 relY:imageHeight - 2 * borderGap];
[MatQuickLook relativeLine:leftBracket relX:lineThickness + lipWidth relY:0];
[MatQuickLook relativeLine:leftBracket relX:0 relY:-lineThickness];
[MatQuickLook relativeLine:leftBracket relX:-lipWidth relY:0];
[MatQuickLook relativeLine:leftBracket relX:0 relY:-(imageHeight - 2 * (borderGap + lineThickness))];
[MatQuickLook relativeLine:leftBracket relX:lipWidth relY:0];
[MatQuickLook relativeLine:leftBracket relX:0 relY:-lineThickness];
[leftBracket closePath];
CGAffineTransform reflect = CGAffineTransformConcat(CGAffineTransformMakeTranslation(-imageWidth, 0), CGAffineTransformMakeScale(-1, 1));
UIBezierPath* rightBracket = [leftBracket copy];
[rightBracket applyTransform:reflect];
CGRect rect = CGRectMake(0, 0, imageWidth, imageHeight);
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0);
[UIColor.whiteColor setFill];
UIRectFill(rect);
[UIColor.blackColor setFill];
[leftBracket fill];
[rightBracket fill];
[labels enumerateObjectsUsingBlock:^(id label, NSUInteger index, BOOL *stop)
{
CGRect boundingRect = boundingRects[label].CGRectValue;
int row = (int)index / [mat cols];
int col = (int)index % [mat cols];
int x = borderGap + lipWidth + col * (maxWidth + colGap) + (maxWidth - boundingRect.size.width) / 2;
int y = borderGap + lipWidth + row * (maxHeight + rowGap) + (maxHeight - boundingRect.size.height) / 2;
CGRect textRect = CGRectMake(x, y, boundingRect.size.width, boundingRect.size.height);
[label drawInRect:textRect withAttributes:textFontAttributes];
}];
UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
} else if (([mat dims] == 2) && ([mat type] == CV_8U || [mat type] == CV_8UC3 || [mat type] == CV_8UC4)) {
return [mat toUIImage];
} else if ([mat dims] == 2 && [mat channels] == 1) {
Mat* normalized = [Mat new];
[Core normalize:mat dst:normalized alpha:0 beta:255 norm_type:NORM_MINMAX dtype:CV_8U];
Mat* normalizedKey = [[Mat alloc] initWithRows:[mat rows] + 10 cols:[mat cols] type:CV_8U];
std::vector<char> key;
for (int index = 0; index < [mat cols]; index++) {
key.push_back((char)(index * 256 / [mat cols]));
}
for (int index = 0; index < 10; index++) {
[normalizedKey put:@[[NSNumber numberWithInt:index], [NSNumber numberWithInt:0]] count:[mat cols] byteBuffer:key.data()];
}
[normalized copyTo:[normalizedKey submatRoi:[[Rect2i alloc] initWithX:0 y:10 width:[mat cols] height:[mat rows]]]];
Mat* colorMap = [Mat new];
[Imgproc applyColorMap:normalizedKey dst:colorMap colormap:COLORMAP_JET];
[Imgproc cvtColor:colorMap dst:colorMap code:COLOR_BGR2RGB];
return [colorMap toUIImage];
}
return [mat description];
}
@end
@@ -0,0 +1,32 @@
//
// Mat+Converters.h
//
// Created by Masaya Tsuruta on 2020/10/08.
//
#pragma once
#ifdef __cplusplus
#import "opencv2/core.hpp"
#else
#define CV_EXPORTS
#endif
#import "Mat.h"
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
NS_ASSUME_NONNULL_BEGIN
CV_EXPORTS @interface MatConverters : NSObject
+(CGImageRef)convertMatToCGImageRef:(Mat*)mat CF_RETURNS_RETAINED;
+(Mat*)convertCGImageRefToMat:(CGImageRef)image;
+(Mat*)convertCGImageRefToMat:(CGImageRef)image alphaExist:(BOOL)alphaExist;
+(NSImage*)converMatToNSImage:(Mat*)mat;
+(Mat*)convertNSImageToMat:(NSImage*)image;
+(Mat*)convertNSImageToMat:(NSImage*)image alphaExist:(BOOL)alphaExist;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,40 @@
//
// MatConverters.mm
//
// Created by Masaya Tsuruta on 2020/10/08.
//
#import "MatConverters.h"
#import <opencv2/imgcodecs/macosx.h>
@implementation MatConverters
+(CGImageRef)convertMatToCGImageRef:(Mat*)mat {
return MatToCGImage(mat.nativeRef);
}
+(Mat*)convertCGImageRefToMat:(CGImageRef)image {
return [MatConverters convertCGImageRefToMat:image alphaExist:NO];
}
+(Mat*)convertCGImageRefToMat:(CGImageRef)image alphaExist:(BOOL)alphaExist {
Mat* mat = [Mat new];
CGImageToMat(image, mat.nativeRef, (bool)alphaExist);
return mat;
}
+(NSImage*)converMatToNSImage:(Mat*)mat {
return MatToNSImage(mat.nativeRef);
}
+(Mat*)convertNSImageToMat:(NSImage*)image {
return [MatConverters convertNSImageToMat:image alphaExist:NO];
}
+(Mat*)convertNSImageToMat:(NSImage*)image alphaExist:(BOOL)alphaExist {
Mat* mat = [Mat new];
NSImageToMat(image, mat.nativeRef, (bool)alphaExist);
return mat;
}
@end
@@ -0,0 +1,27 @@
//
// MatQuickLook.h
//
// Created by Giles Payne on 2021/07/18.
//
#pragma once
#ifdef __cplusplus
#import "opencv2/core.hpp"
#else
#define CV_EXPORTS
#endif
#import "Mat.h"
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
NS_ASSUME_NONNULL_BEGIN
CV_EXPORTS @interface MatQuickLook : NSObject
+ (id)matDebugQuickLookObject:(Mat*)mat;
@end
NS_ASSUME_NONNULL_END
@@ -0,0 +1,158 @@
//
// MatQuickLook.mm
//
// Created by Giles Payne on 2021/07/18.
//
#import "MatQuickLook.h"
#import "MatConverters.h"
#import "Rect2i.h"
#import "Core.h"
#import "Imgproc.h"
#import <opencv2/imgcodecs/macosx.h>
#define SIZE 20
static NSFont* getCMU() {
return [NSFont fontWithName:@"CMU Serif" size:SIZE];
}
static NSFont* getBodoni72() {
return [NSFont fontWithName:@"Bodoni 72" size:SIZE];
}
static NSFont* getAnySerif() {
#if defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 110000
if (@available(macOS 11.0, *)) {
return [NSFont fontWithDescriptor:[[NSFontDescriptor preferredFontDescriptorForTextStyle:NSFontTextStyleBody options:@{}] fontDescriptorWithDesign:NSFontDescriptorSystemDesignSerif] size:SIZE];
} else {
return nil;
}
#else
return nil;
#endif
}
static NSFont* getSystemFont() {
return [NSFont systemFontOfSize:SIZE];
}
typedef NSFont* (*FontGetter)();
@implementation MatQuickLook
+ (NSString*)makeLabel:(BOOL)isIntType val:(NSNumber*)num {
if (isIntType) {
return [NSString stringWithFormat:@"%d", num.intValue];
} else {
int exponent = 1 + (int)log10(abs(num.doubleValue));
if (num.doubleValue == (double)num.intValue && num.doubleValue < 10000 && num.doubleValue > -10000) {
return [NSString stringWithFormat:@"%d", num.intValue];;
} else if (exponent <= 5 && exponent >= -1) {
return [NSString stringWithFormat:[NSString stringWithFormat:@"%%%d.%df", 6, MIN(5 - exponent, 4)], num.doubleValue];
} else {
return [[[NSString stringWithFormat:@"%.2e", num.doubleValue] stringByReplacingOccurrencesOfString:@"e+0" withString:@"e"] stringByReplacingOccurrencesOfString:@"e-0" withString:@"e-"];
}
}
}
+ (id)matDebugQuickLookObject:(Mat*)mat {
// for smallish Mat objects display as a matrix
if ([mat dims] == 2 && [mat rows] <= 10 && [mat cols] <= 10 && [mat channels] == 1) {
FontGetter fontGetters[] = { getCMU, getBodoni72, getAnySerif, getSystemFont };
NSFont* font = nil;
for (int fontGetterIndex = 0; font==nil && fontGetterIndex < (sizeof(fontGetters)) / (sizeof(fontGetters[0])); fontGetterIndex++) {
font = fontGetters[fontGetterIndex]();
}
int elements = [mat rows] * [mat cols];
NSDictionary<NSAttributedStringKey,id>* textFontAttributes = @{ NSFontAttributeName: font, NSForegroundColorAttributeName: NSColor.blackColor };
NSMutableArray<NSNumber*>* rawData = [NSMutableArray new];
for (int dataIndex = 0; dataIndex < elements; dataIndex++) {
[rawData addObject:[NSNumber numberWithDouble:0]];
}
[mat get:0 col: 0 data: rawData];
BOOL isIntType = [mat depth] <= CV_32S;
NSMutableArray<NSString*>* labels = [NSMutableArray new];
NSMutableDictionary<NSString*, NSValue*>* boundingRects = [NSMutableDictionary dictionaryWithCapacity:elements];
int maxWidth = 0, maxHeight = 0;
for (NSNumber* number in rawData) {
NSString* label = [MatQuickLook makeLabel:isIntType val:number];
[labels addObject:label];
NSRect boundingRect = [label boundingRectWithSize:NSMakeSize(CGFLOAT_MAX, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:textFontAttributes];
if (boundingRect.size.width > maxWidth) {
maxWidth = boundingRect.size.width;
}
if (boundingRect.size.height > maxHeight) {
maxHeight = boundingRect.size.height;
}
boundingRects[label] = [NSValue valueWithRect:boundingRect];
}
int rowGap = 8;
int colGap = 8;
int borderGap = 9;
int lineThickness = 4;
int lipWidth = 8;
int imageWidth = 2 * (borderGap + lipWidth) + maxWidth * [mat cols] + colGap * ([mat cols] - 1);
int imageHeight = 2 * (borderGap + lipWidth) + maxHeight * [mat rows] + rowGap * ([mat rows] - 1);
NSImage* image = [[NSImage alloc] initWithSize:NSMakeSize(imageWidth, imageHeight)];
NSBezierPath* leftBracket = [NSBezierPath new];
[leftBracket moveToPoint:NSMakePoint(borderGap, borderGap)];
[leftBracket relativeLineToPoint:NSMakePoint(0, imageHeight - 2 * borderGap)];
[leftBracket relativeLineToPoint:NSMakePoint(lineThickness + lipWidth, 0)];
[leftBracket relativeLineToPoint:NSMakePoint(0, -lineThickness)];
[leftBracket relativeLineToPoint:NSMakePoint(-lipWidth, 0)];
[leftBracket relativeLineToPoint:NSMakePoint(0, -(imageHeight - 2 * (borderGap + lineThickness)))];
[leftBracket relativeLineToPoint:NSMakePoint(lipWidth, 0)];
[leftBracket relativeLineToPoint:NSMakePoint(0, -lineThickness)];
[leftBracket relativeLineToPoint:NSMakePoint(-(lineThickness + lipWidth), 0)];
NSAffineTransform* reflect = [NSAffineTransform new];
[reflect scaleXBy:-1 yBy:1];
[reflect translateXBy:-imageWidth yBy:0];
NSBezierPath* rightBracket = [leftBracket copy];
[rightBracket transformUsingAffineTransform:reflect];
[image lockFocus];
[NSColor.whiteColor drawSwatchInRect:NSMakeRect(0, 0, imageWidth, imageHeight)];
[NSColor.blackColor set];
[leftBracket fill];
[rightBracket fill];
[labels enumerateObjectsUsingBlock:^(id label, NSUInteger index, BOOL *stop)
{
NSRect boundingRect = boundingRects[label].rectValue;
int row = [mat rows] - 1 - ((int)index / [mat cols]);
int col = (int)index % [mat cols];
int x = borderGap + lipWidth + col * (maxWidth + colGap) + (maxWidth - boundingRect.size.width) / 2;
int y = borderGap + lipWidth + row * (maxHeight + rowGap) + (maxHeight - boundingRect.size.height) / 2;
NSRect textRect = NSMakeRect(x, y, boundingRect.size.width, boundingRect.size.height);
[label drawInRect:textRect withAttributes:textFontAttributes];
}];
[image unlockFocus];
return image;
} else if (([mat dims] == 2) && ([mat type] == CV_8U || [mat type] == CV_8UC3 || [mat type] == CV_8UC4)) {
// convert to NSImage if the Mats has 2 dimensions and a type and number of channels consistent with it being a image
return [mat toNSImage];
} else if ([mat dims] == 2 && [mat channels] == 1) {
// for other Mats with 2 dimensions and one channel - generate heat map
Mat* normalized = [Mat new];
[Core normalize:mat dst:normalized alpha:0 beta:255 norm_type:NORM_MINMAX dtype:CV_8U];
Mat* normalizedKey = [[Mat alloc] initWithRows:[mat rows] + 10 cols:[mat cols] type:CV_8U];
std::vector<char> key;
for (int index = 0; index < [mat cols]; index++) {
key.push_back((char)(index * 256 / [mat cols]));
}
for (int index = 0; index < 10; index++) {
[normalizedKey put:@[[NSNumber numberWithInt:index], [NSNumber numberWithInt:0]] count:[mat cols] byteBuffer:key.data()];
}
[normalized copyTo:[normalizedKey submatRoi:[[Rect2i alloc] initWithX:0 y:10 width:[mat cols] height:[mat rows]]]];
Mat* colorMap = [Mat new];
[Imgproc applyColorMap:normalizedKey dst:colorMap colormap:COLORMAP_JET];
[Imgproc cvtColor:colorMap dst:colorMap code:COLOR_BGR2RGB];
return [colorMap toNSImage];
}
//everything just return the Mat description
return [mat description];
}
@end
@@ -0,0 +1,50 @@
//
// Imgcodecs.swift
//
// Created by Giles Payne on 2020/02/10.
//
import XCTest
import OpenCV
class ImgcodecsTest: OpenCVTestCase {
let LENA_PATH = Bundle(for: ImgcodecsTest.self).path(forResource:"lena", ofType:"png", inDirectory:"resources")!
func testImencodeStringMatListOfByte() {
var buff = [UInt8]()
XCTAssert(Imgcodecs.imencode(ext: ".jpg", img: gray127, buf: &buff))
XCTAssertFalse(0 == buff.count)
}
func testImencodeStringMatListOfByteListOfInteger() {
let params40:[Int32] = [ImwriteFlags.IMWRITE_JPEG_QUALITY.rawValue, 40]
let params90:[Int32] = [ImwriteFlags.IMWRITE_JPEG_QUALITY.rawValue, 90]
var buff40 = [UInt8]()
var buff90 = [UInt8]()
XCTAssert(Imgcodecs.imencode(ext: ".jpg", img: rgbLena, buf: &buff40, params: params40))
XCTAssert(Imgcodecs.imencode(ext: ".jpg", img: rgbLena, buf: &buff90, params: params90))
XCTAssert(buff40.count > 0)
XCTAssert(buff40.count < buff90.count)
}
func testImreadString() {
dst = Imgcodecs.imread(filename: LENA_PATH)
XCTAssertFalse(dst.empty())
XCTAssertEqual(3, dst.channels())
XCTAssert(512 == dst.cols())
XCTAssert(512 == dst.rows())
}
func testImreadStringInt() {
dst = Imgcodecs.imread(filename: LENA_PATH, flags: 0)
XCTAssertFalse(dst.empty());
XCTAssertEqual(1, dst.channels());
XCTAssert(512 == dst.cols());
XCTAssert(512 == dst.rows());
}
}