2017-04-23 88 views
0

我有一個應用程序可以對圖像的像素進行計數,以獲取圖像中五種顏色的相對區域。 Swift 3的更新版本與聲明的圖像一起使用,該圖像位於應用程序的支持文件中,但沒有來自文檔目錄的可變圖像。我錯過了什麼?在Swift中進行像素計數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 
    } 
} 
} 
+0

「ComputeAreaViewController」類中究竟發生了什麼不起作用?如果我們確切地知道這是什麼不起作用會很有幫助 – KSigWyatt

+0

真誠的問題 - 您是否嘗試過使用HD圖像?一個大的圖像?任何性能問題?有關使用CIAreaHistogram的想法嗎? – dfd

+0

使用包含「let image = UIImage(named:」test-image.png「)的版本,computeArea函數可以很好地工作,並且像素被計數爲給出結果。使用」var image:UIImage!「版本獲取圖像從文件目錄和顯示,但computeArea函數不起作用,我得到的全部是零。我懷疑它與「view.layer.contents = image:cgImage!」有關「 – user1698875

回答

0

答案是正在傳遞的圖像的alpha通道。它低於1.0,將其重置爲1.0可以解決計數問題。