0
이미지의 픽셀 수를 계산하여 이미지에서 5 가지 색상의 상대적 영역을 얻는 앱이 있습니다. Swift 3의 업데이트 된 버전은 응용 프로그램의 지원 파일에는 있지만 문서 디렉토리에서 오는 가변 이미지에는없는 선언 된 이미지로 작동합니다. 내가 뭘 놓치고 있니?스위프트에서 픽셀 카운팅 3
이 버전은 작동합니다
import UIKit
import CoreGraphics
class ComputeAreaViewController: UIViewController {
var redValue: String!
var blueValue: String!
var greenValue: String!
var whiteValue: String!
var yellowValue: String!
var totalValue: String!
//var image: UIImage!
let image = UIImage(named: "test_image.png")!
override func viewDidLoad() {
super.viewDidLoad()
/*
let fm = FileManager.default
let docsurl = try! fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let myurl = docsurl.appendingPathComponent("tmp.png")
image = UIImage(contentsOfFile: myurl.path)!
*/
view.layer.contents = image.cgImage!
computeArea()
}
func classifyPixel(_ r: UInt8, _ g: UInt8, _ b: UInt8, _ coverRed: inout UInt, _ coverGreen: inout UInt, _ coverYellow: inout UInt, _ coverBlue: inout UInt, _ coverWhite: inout UInt) {
if r == 255 && g == 0 && b == 0 { coverRed += 1 }
if r == 0 && g == 255 && b == 0 { coverGreen += 1 }
if r == 255 && g == 255 && b == 0 { coverYellow += 1 }
if r == 0 && g == 0 && b == 255 { coverBlue += 1 }
if r == 255 && g == 255 && b == 255 { coverWhite += 1 }
}
func computeArea() {
var coverRed: UInt = 0
var coverGreen: UInt = 0
var coverYellow: UInt = 0
var coverBlue: UInt = 0
var coverWhite: UInt = 0
var coverTotal = 0
let imageWidth = Int(image.size.width)
let imageHeight = Int(image.size.height)
let bitsPerComponent = 8
let bytesPerPixel = 4
let bytesPerRow = bytesPerPixel * imageWidth
let bufferSize = bytesPerRow * imageHeight
var bytesPointer = UnsafeMutableRawPointer.allocate(bytes: bufferSize, alignedTo: 1)
let bytesPointerSaved = bytesPointer
if let bitmapContext = CGContext(data: bytesPointer, width: imageWidth, height: imageHeight, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) {
bitmapContext.draw((image.cgImage!), in: CGRect(x: 0, y: 0, width: imageWidth, height: imageHeight), byTiling: false)
for _ in 0..<imageHeight {
for _ in 0..<imageWidth {
var r: UInt8 = 0, g: UInt8 = 0, b: UInt8 = 0
r = bytesPointer.load(as: UInt8.self)
bytesPointer += 1
g = bytesPointer.load(as: UInt8.self)
bytesPointer += 1
b = bytesPointer.load(as: UInt8.self)
bytesPointer += 2
classifyPixel(r, g, b, &coverRed, &coverGreen, &coverYellow, &coverBlue, &coverWhite)
coverTotal += 1
}
}
} else {
print(#file + " " + #function + " failed to create bitmap context ")
}
bytesPointerSaved.deallocate(bytes: bufferSize, alignedTo: 1)
let fTotal = Float(coverTotal)
print("coverRed: \(100.0*Float(coverRed)/fTotal), coverGreen: \ (100.0*Float(coverGreen)/fTotal), coverYellow: \(100.0*Float(coverYellow)/fTotal), coverBlue: \(100.0*Float(coverBlue)/fTotal), coverWhite: \(100.0*Float(coverWhite)/fTotal), coverTotal: \(coverTotal), #ofPixels: \(bufferSize/bytesPerPixel)")
greenValue = String(describing: (100.0*Float(coverGreen)/fTotal))
yellowValue = String(describing: (100.0*Float(coverYellow)/fTotal))
redValue = String(describing: (100.0*Float(coverRed)/fTotal))
blueValue = String(describing: (100.0*Float(coverBlue)/fTotal))
whiteValue = String(describing: (100.0*Float(coverWhite)/fTotal))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "computeResult" {
let areaController = segue.destination as! AreaPercentViewController
areaController.redValue = redValue
areaController.blueValue = blueValue
areaController.greenValue = greenValue
areaController.whiteValue = whiteValue
areaController.yellowValue = yellowValue
areaController.totalValue = totalValue
}
}
}
이 버전은 작동하지 않습니다
import UIKit
import CoreGraphics
class ComputeAreaViewController: UIViewController {
var redValue: String!
var blueValue: String!
var greenValue: String!
var whiteValue: String!
var yellowValue: String!
var totalValue: String!
var image: UIImage!
//let image = UIImage(named: "test_image.png")!
override func viewDidLoad() {
super.viewDidLoad()
let fm = FileManager.default
let docsurl = try! fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let myurl = docsurl.appendingPathComponent("tmp.png")
image = UIImage(contentsOfFile: myurl.path)!
view.layer.contents = image.cgImage!
computeArea()
}
func classifyPixel(_ r: UInt8, _ g: UInt8, _ b: UInt8, _ coverRed: inout UInt, _ coverGreen: inout UInt, _ coverYellow: inout UInt, _ coverBlue: inout UInt, _ coverWhite: inout UInt) {
if r == 255 && g == 0 && b == 0 { coverRed += 1 }
if r == 0 && g == 255 && b == 0 { coverGreen += 1 }
if r == 255 && g == 255 && b == 0 { coverYellow += 1 }
if r == 0 && g == 0 && b == 255 { coverBlue += 1 }
if r == 255 && g == 255 && b == 255 { coverWhite += 1 }
}
func computeArea() {
var coverRed: UInt = 0
var coverGreen: UInt = 0
var coverYellow: UInt = 0
var coverBlue: UInt = 0
var coverWhite: UInt = 0
var coverTotal = 0
let imageWidth = Int(image.size.width)
let imageHeight = Int(image.size.height)
let bitsPerComponent = 8
let bytesPerPixel = 4
let bytesPerRow = bytesPerPixel * imageWidth
let bufferSize = bytesPerRow * imageHeight
var bytesPointer = UnsafeMutableRawPointer.allocate(bytes: bufferSize, alignedTo: 1)
let bytesPointerSaved = bytesPointer
if let bitmapContext = CGContext(data: bytesPointer, width: imageWidth, height: imageHeight, bitsPerComponent: bitsPerComponent, bytesPerRow: bytesPerRow, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) {
bitmapContext.draw((image.cgImage!), in: CGRect(x: 0, y: 0, width: imageWidth, height: imageHeight), byTiling: false)
for _ in 0..<imageHeight {
for _ in 0..<imageWidth {
var r: UInt8 = 0, g: UInt8 = 0, b: UInt8 = 0
r = bytesPointer.load(as: UInt8.self)
bytesPointer += 1
g = bytesPointer.load(as: UInt8.self)
bytesPointer += 1
b = bytesPointer.load(as: UInt8.self)
bytesPointer += 2
classifyPixel(r, g, b, &coverRed, &coverGreen, &coverYellow, &coverBlue, &coverWhite)
coverTotal += 1
}
}
} else {
print(#file + " " + #function + " failed to create bitmap context ")
}
bytesPointerSaved.deallocate(bytes: bufferSize, alignedTo: 1)
let fTotal = Float(coverTotal)
print("coverRed: \(100.0*Float(coverRed)/fTotal), coverGreen: \(100.0*Float(coverGreen)/fTotal), coverYellow: \(100.0*Float(coverYellow)/fTotal), coverBlue: \(100.0*Float(coverBlue)/fTotal), coverWhite: \(100.0*Float(coverWhite)/fTotal), coverTotal: \(coverTotal), #ofPixels: \(bufferSize/bytesPerPixel)")
greenValue = String(describing: (100.0*Float(coverGreen)/fTotal))
yellowValue = String(describing: (100.0*Float(coverYellow)/fTotal))
redValue = String(describing: (100.0*Float(coverRed)/fTotal))
blueValue = String(describing: (100.0*Float(coverBlue)/fTotal))
whiteValue = String(describing: (100.0*Float(coverWhite)/fTotal))
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "computeResult" {
let areaController = segue.destination as! AreaPercentViewController
areaController.redValue = redValue
areaController.blueValue = blueValue
areaController.greenValue = greenValue
areaController.whiteValue = whiteValue
areaController.yellowValue = yellowValue
areaController.totalValue = totalValue
}
}
}
정확하게 'ComputeAreaViewController'클래스에서 작동하지 않는 것은 무엇입니까? 우리가 정확하게 작동하지 않는다는 것을 알고 있다면 도움이 될 것입니다. – KSigWyatt
진심으로 질문 - HD 이미지로 시도해 보셨습니까? 큰 이미지? 성능 문제가 있습니까? 대신 CIAreaHistogram을 사용하는 것에 대한 생각은? – dfd
"let image = UIImage (named :"test-image.png ")가 포함 된 버전에서는 computeArea 함수가 완벽하게 작동하고 픽셀이 계산되어 결과가 나타납니다."var image : UIImage! "버전에서는 이미지를 가져옵니다 문서 디렉토리에서 표시 및 표시하지만 computeArea 함수가 작동하지 않고 내가 얻는 것은 모두 0입니다. "view.layer.contents = image : cgImage!"와 관련이 있다고 의심됩니다. – user1698875