0

I'd like to use the DeepLearningKit for iOS. I want to use UIImage objects to be classified. The sample application only uses a float array loaded from a json file. Thus I have to create the bitmap representation of the UIImage as a float array and use this for the classify-method.

Can anybody help me on that? Is there a way to create a bitmap representation for UIImage? Moreover I have to swap the channels from RGB to BGR.

Thank you

2 Answers 2

0

Have added an extension to UIImage that allows setting and getting RGB(A) pixels directly - key methods:

public func setPixelColorAtPoint(point:CGPoint, color: RawColorType) -> UIImage? 
func getPixelColorAtLocation(point:CGPoint)->UIColor?

where RawColorType is defined as

public typealias RawColorType = (newRedColor:UInt8, newgreenColor:UInt8, newblueColor:UInt8,  newalphaValue:UInt8)

This way you should be able to convert back and forth between bitmap representation and UIImage. Wrote a blog post that gives some more context: http://deeplearningkit.org/tutorials-for-ios-os-x-and-tvos/tutorial-image-handling-in-deeplearningkit/

Sign up to request clarification or add additional context in comments.

1 Comment

Downvoted as link no longer available.
0

I've write a function to convert a image file to Caffe blob on iOS platform. You can find it here. I hope it will help you.

Code snippet:

// Convert Bitmap (channels*width*height) to Matrix (width*height*channels)
// Remove alpha channel
int input_channels = input_layer->channels();
LOG(INFO) << "image_channels:" << image_channels << " input_channels:" << input_channels;
if (input_channels == 3 && image_channels != 4) {
    LOG(ERROR) << "image_channels input_channels not match.";
    return false;
} else if (input_channels == 1 && image_channels != 1) {
    LOG(ERROR) << "image_channels input_channels not match.";
    return false;
}

float *input_data = input_layer->mutable_cpu_data();

for (size_t h = 0; h < height; h++) {
    for (size_t w = 0; w < width; w++) {
        for (size_t c = 0; c < input_channels; c++) {
            // OpenCV use BGR instead of RGB
            size_t cc = c;
            if (input_channels == 3) {
                cc = 2 - c;
            }
            // Convert uint8_t to float
            input_data[c*width*height + h*width + w] = 
                static_cast<float>(result[h*width*image_channels + w*image_channels + cc]);
            if (mean.size() == input_channels) {
                input_data[c*width*height + h*width + w] -= mean[c];
            }
        }
    }
}

1 Comment

The question is tagged "Swift", though... not "Objective-C++". :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.