Image processing iOS视觉检测二值化图像上的光
我遇到了一些问题。如何定义二值化图像上的光斑。我现在正在使用ios11和Vision 我使用二值化过滤器Image processing iOS视觉检测二值化图像上的光,image-processing,computer-vision,core-image,ios11,coreml,Image Processing,Computer Vision,Core Image,Ios11,Coreml,我遇到了一些问题。如何定义二值化图像上的光斑。我现在正在使用ios11和Vision 我使用二值化过滤器CIColorControls(还尝试将其与CIColorInvert结合使用)。 对于灯光检测,我将VNImageRequestHandler与vndetetractanglesrequest一起使用。 在vndetecatedtractanglesrequest中,我检查vndecatedObjectObservation 但无法实现100%的帧检测(有时应用程序无法识别帧上的光点)。我做
CIColorControls
(还尝试将其与CIColorInvert
结合使用)。对于灯光检测,我将
VNImageRequestHandler
与vndetetractanglesrequest
一起使用。
在vndetecatedtractanglesrequest
中,我检查vndecatedObjectObservation
但无法实现100%的帧检测(有时应用程序无法识别帧上的光点)。我做错了什么?谢谢你的帮助
这是我的密码
lazy var rectanglesRequest: VNDetectRectanglesRequest = {
return VNDetectRectanglesRequest(completionHandler: self.handleRectangles)
}()
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
connection.videoOrientation = AVCaptureVideoOrientation.portrait
guard let uiImage = imageFromSampleBuffer(sampleBuffer: sampleBuffer) else { return }
let correctedImage = uiImage
.applyingFilter("CIColorControls", withInputParameters: [
kCIInputSaturationKey: 0,
kCIInputContrastKey: 4.5,
kCIInputBrightnessKey: -1.54
])
//.applyingFilter("CIColorInvert", withInputParameters: nil)
self.searchLightSpot(ciImage: correctedImage)
DispatchQueue.main.async { [unowned self] in //unowned
self.frameImageView.image = UIImage(ciImage: correctedImage)
}
}
func searchLightSpot(ciImage: CIImage) {
var requestOptions: [VNImageOption: Any] = [:]
let handler = VNImageRequestHandler(ciImage: ciImage, options: requestOptions)
DispatchQueue.global(qos: .userInteractive).async {
do {
try handler.perform([self.rectanglesRequest])
} catch {
print(error)
}
}
}
func handleRectangles(request: VNRequest, error: Error?) {
guard let observations = request.results as? [VNDetectedObjectObservation]
else {
print("unexpected result type from VNDetectedObjectObservation")
return
}
guard let detectedObject = observations.first else {
print("not detected object")
return
}
print("detected object: ", detectedObject)
}
在做了额外的研究之后,我了解到苹果似乎对框架进行了额外的优化 例如,如果我们在第一帧中获得灯光,那么接下来的5帧将是相同的 它将显示相同的帧,因此取而代之的是5帧-我们只有1帧 这意味着我们不能100%确定地依赖于每秒的静态帧数 所以我无法检测信号的持续时间等等 可能的解决方案之一是: 1。我们可以启动计时器,从相机获取当前帧(例如每100ms一次) 2。然后我们将检查框架是否有白点。基于这些Historogram的结果 y-颜色(如果框架上存在白色,则显示0/1) x-时间线(毫秒) 3。所以这个图表的输出可能是
[01111100001111…]
4。然后我们可以分析和检测信号
你可以用
并编写自己的扩展来进行二值化和检测白点
extension Image where Pixel == RGBA {
fileprivate func getPixelCount() -> Int {
return Int(10 * width / 100)
}
func binarize() -> (isWhite: Bool, binarizedImage: Image<RGBA>) {
var kWidth = 0
var img = self
let pixelCount = getPixelCount()
for x in 0..<width{
var kHeight = 0
for y in 0..<height {
if let _pixel = pixel(x, y) {
if _pixel.gray < 245 {
img[x, y] = .black
kHeight = 0
} else {
img[x, y] = .white
kHeight += 1
}
if kHeight > pixelCount {
kWidth += 1
break
}
}
}
print("Hwhite: \(kHeight) Wwhite: \(kWidth)")
if kHeight >= pixelCount && kWidth >= pixelCount {
return (true, img)
}
}
return (false, img)
}
}
扩展图像,其中像素==RGBA{
fileprivate func getPixelCount()->Int{
返回整数(10*宽度/100)
}
func binarize()->(isWhite:Bool,binarizedImage:Image){
var kWidth=0
var img=自我
让pixelCount=getPixelCount()
对于0中的x..=pixelCount&&kWidth>=pixelCount{
返回(true,img)
}
}
返回(假,img)
}
}
在做了额外的研究之后,我了解到苹果似乎对框架进行了额外的优化
例如,如果我们在第一帧中获得灯光,那么接下来的5帧将是相同的
它将显示相同的帧,因此取而代之的是5帧-我们只有1帧
这意味着我们不能100%确定地依赖于每秒的静态帧数
所以我无法检测信号的持续时间等等
可能的解决方案之一是:
1。我们可以启动计时器,从相机获取当前帧(例如每100ms一次)
2。然后我们将检查框架是否有白点。基于这些Historogram的结果
y-颜色(如果框架上存在白色,则显示0/1)
x-时间线(毫秒)
3。所以这个图表的输出可能是
[01111100001111…]
4。然后我们可以分析和检测信号
你可以用
并编写自己的扩展来进行二值化和检测白点
extension Image where Pixel == RGBA {
fileprivate func getPixelCount() -> Int {
return Int(10 * width / 100)
}
func binarize() -> (isWhite: Bool, binarizedImage: Image<RGBA>) {
var kWidth = 0
var img = self
let pixelCount = getPixelCount()
for x in 0..<width{
var kHeight = 0
for y in 0..<height {
if let _pixel = pixel(x, y) {
if _pixel.gray < 245 {
img[x, y] = .black
kHeight = 0
} else {
img[x, y] = .white
kHeight += 1
}
if kHeight > pixelCount {
kWidth += 1
break
}
}
}
print("Hwhite: \(kHeight) Wwhite: \(kWidth)")
if kHeight >= pixelCount && kWidth >= pixelCount {
return (true, img)
}
}
return (false, img)
}
}
扩展图像,其中像素==RGBA{
fileprivate func getPixelCount()->Int{
返回整数(10*宽度/100)
}
func binarize()->(isWhite:Bool,binarizedImage:Image){
var kWidth=0
var img=自我
让pixelCount=getPixelCount()
对于0中的x..=pixelCount&&kWidth>=pixelCount{
返回(true,img)
}
}
返回(假,img)
}
}