FLIR One Developer / Blog / Topics / Palette, Swift / How to change the palette in swift?

How to change the palette in swift?

Home Forums iOS development Installation and getting started How to change the palette in swift?

Tagged: ,

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of Martinez Martinez 1 day, 7 hours ago.

Viewing 6 posts - 1 through 6 (of 6 total)

  • Author

    Posts

  • #7985

    Profile photo of Cem Dogan
    Cem Dogan

    Participant

    Hi everyone,</p>
    <p>I would be thankful if someone helped me about how to change the palette from iron to grayscale in Swift 4</p>
    <p>Cem

    • This topic was modified 11 months ago by Profile photo of Cem Dogan Cem Dogan.

    #7993

    Profile photo of Dyra
    Dyra

    Participant

    You can use CIColorControls and set Contrast Key kCIInputContrastKey to increase the black/white contrast as follow:

    To convert from colors to grayscale you can set the Saturation Key kCIInputSaturationKey to zero:\

    extension CIImage {
    var image: UIImage? {
    let image = UIImage(ciImage: self)
    UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
    defer { UIGraphicsEndImageContext() }
    image.draw(in: CGRect(origin: .zero, size: image.size))
    return UIGraphicsGetImageFromCurrentImageContext()
    }
    }
    extension UIImage {
    func applying(saturation value: NSNumber) -> UIImage? {
    return CIImage(image: self)?
    .applyingFilter(“CIColorControls”, parameters: [kCIInputSaturationKey: value])
    .image
    } // Swift 3 uses withInputParameters instead of parameters
    var grayscale: UIImage? {
    return applying(saturation: 0)
    }
    }
    ____________________________________________________________________________________________

    let link = “https://i.stack.imgur.com/Xs4RX.jpg”
    let image = UIImage(data: try! Data(contentsOf: URL(string: link)!))
    if let grayscale = image?.grayscale {
    // use grayscale image here
    }

    OR

    Desaturate will convert your image to grayscale
    Increasing the contrast will push those grays out to the extremes, i.e. black and white.

    let ciImage = CIImage(image: image)!
    let blackAndWhiteImage = ciImage.applyingFilter(“CIColorControls”, withInputParameters: [“inputSaturation”: 0, “inputContrast”: 5])

    and done.

    Thanks.

    #7998

    Profile photo of Dyra
    Dyra

    Participant

    Did this useful?
    vidmate

    #8000

    Profile photo of Cem Dogan
    Cem Dogan

    Participant

    Hi,

    Thanks for your help. It seems to me a good solution but what I was trying to ask is to find a way that I can change the palette with the FLIR SDK kind of code in Swift. There is a sample code in the example app but it is in Objective-C and my coding capabilities are not that much higher to translate it into swift. I tried, tried and tried but couldn’t come up with a solution.

    Is there a solution that you know in that sense?

    Best,

    Cem

    #10412

    Profile photo of Scott
    Scott

    Participant

    This is what I’m using in my current Swift implementation. I set the pallet in the imageReceived() callback before requesting the image from the instance of FLIRThermalImage.

    func imageReceived() {
            
            DispatchQueue.main.async { [weak self] in
                
                guard let self = self else { return }
                
                MultiPhotoFLIRCamera.camera.withImage({ [weak self] thermalImage in
                    
                    guard let self2 = self else { return }
                    
                    thermalImage.palette = thermalImage.paletteManager.iron
                    let image = thermalImage.getImage()
                    self2.imageView.image = image
                })
            }
        }
    
    • This reply was modified 4 days, 3 hours ago by Profile photo of Scott Scott.
    • This reply was modified 4 days, 3 hours ago by Profile photo of Scott Scott.
    • This reply was modified 4 days, 3 hours ago by Profile photo of Scott Scott.

    #10447

    Profile photo of Martinez
    Martinez

    Participant

    Firstly, accessing colors from a centralized location is convenient. It eliminates the hassle of hunting for specific RGB values each time they’re needed to configure a UI element.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.