0
CoreGraphicsまたはCoreImageおよびSwift3でイメージをトリミング/トリミングする簡単な方法があるのでしょうか?Swift3 - CoreGraphicsまたはCoreImageを使用してイメージから切り抜きまたは切り抜きを行うピクセル
(赤/バラの背景を持つ)上に入力画像があると、それらの赤いピクセルを外して、実際の興味深いビットだけが残るようにします。
CoreGraphicsまたはCoreImageおよびSwift3でイメージをトリミング/トリミングする簡単な方法があるのでしょうか?Swift3 - CoreGraphicsまたはCoreImageを使用してイメージから切り抜きまたは切り抜きを行うピクセル
(赤/バラの背景を持つ)上に入力画像があると、それらの赤いピクセルを外して、実際の興味深いビットだけが残るようにします。
私はちょうどここの上に同様の質問のサブセットに答え:Iterate over individual pixels of a UIImage with height information
をここでは、上部と下部のユニークなピクセルを取得する方法です。
func pixelNotMatchingTopLeftColor(in image: UIImage, first isFirst: Bool) -> CGPoint? {
let width = Int(image.size.width)
let height = Int(image.size.height)
guard width > 1 || height < 1 else {
return nil
}
if let cfData:CFData = image.cgImage?.dataProvider?.data, let pointer = CFDataGetBytePtr(cfData) {
let cornerPixelRed = pointer.pointee
let cornerPixelGreen = pointer.advanced(by: 0).pointee
let cornerPixelBlue = pointer.advanced(by: 1).pointee
let cornerPixelAlpha = pointer.advanced(by: 2).pointee
let bytesPerpixel = 4
let firstPixel = 1 * bytesPerpixel
let lastPixel = width * height * bytesPerpixel - 1 * bytesPerpixel
let range = isFirst ? stride(from: firstPixel, through: lastPixel, by: bytesPerpixel) :
stride(from: lastPixel, through: firstPixel + bytesPerPixel, by: -bytesPerpixel)
for pixelAddress in range {
if pointer.advanced(by: pixelAddress).pointee != cornerPixelRed || //Red
pointer.advanced(by: pixelAddress + 1).pointee != cornerPixelGreen || //Green
pointer.advanced(by: pixelAddress + 2).pointee != cornerPixelBlue || //Blue
pointer.advanced(by: pixelAddress + 3).pointee != cornerPixelAlpha { //Alpha
print(pixelAddress)
return CGPoint(x: (pixelAddress/bytesPerpixel) % width, y: (pixelAddress/bytesPerpixel)/width)
}
}
}
return nil
}
func firstUniquePixel(in image: UIImage) -> CGPoint? {
return pixelNotMatchingTopLeftColor(in: image, first: true)
}
func lastUniquePixel(in image: UIImage) -> CGPoint? {
return pixelNotMatchingTopLeftColor(in: image, first: false)
}
ポインタの数を計算する際に問題がある場合は、私に知らせてください。次に、上、左、下、右を使用して、CGImageCreateWithImageInRectと呼ぶだけです。