Better image coloring logic/algorithm
I am developing an iOS app in which the user can change a part of an image’s color, say a Tea Cup, by touching it. I am using Floodfill algorithm to fill colors so that the user has to tap on the Tea Cup to change its color. That’s working fine. But, the final color looks little different than the replacement color. I have some problem finding out a better logic to convert the object’s(Tea Cup) color to the selected color considering its saturation & lightness.
I am using the following logic to get the result color. I am representing color as (hue, saturation, value).
touchedColor = (tchd_h, tchd_s, tchd_v); // I am not using this now pixelColor = (old_h, old_s, old_v); replacementColor = (new_h, new_s, new_v); resultColor = (new_h, new_s, old_v); pixelColor = resultColor;
The cup before painting (circled with red color).
The selected replacementColor.
Cup after painting the replacementColor (circled with red color).
See the final image above. As I am just changing only the hue & saturation, and not the value of the pixelColor, the applied color doesn’t look similar to the selected replacementColor. The lightness of the image remains unaltered.
If I change the value along with hue & saturation like this,
resultColor = (new_h, new_s, new_v); pixelColor = resultColor;
Then the cup becomes flat colored, missing the lights & shades like this,
I want some idea to tweak the above logic to change the pixel color into a matching replacement color. May be some formula to derive the saturation & value.
- UITableViewCell background color is different in iOS5 vs iOS4
- How to tell Smart Invert in iOS 11 not to invert my app colors and detect if it is enabled?
- Is there a color picker library/code for iPhone development?
- Change UITextField and UITextView Cursor / Caret Color
- UITextField border color
- How to calculate the average color of a UIImage?
Solutions Collect From Internet About “Better image coloring logic/algorithm”
In your example, let’s call the pink color the “Color To Replace,” and let’s call the brown color the “Replacement Color.” For each pixel in the destination, find the corresponding pixel in the source. See how it varies from the “Color to Replace”. Now make similar adjustments to the “Replacement Color” and use that as the color at the current output pixel.
As an example, if the current source pixel is darker than the color to replace by 5 “v” units, then set the output pixel to the replacement color made darker by 5 “v” units. (And you’d want to make the same adjustments in hue and saturation, as well.)
You’ll probably need to limit the range of colors you adjust so you aren’t turning other objects a different color.
- Cocoapods OpenCV 2.4.10 Linker Error
- Swift Generic constraints in init
- UITableView goes under translucent Navigation Bar
- How do I type using my keyboard on the iphone simulator?
- Required Background modes key is not there in info-plist file
- What can cause this SIGSEGV error?
- When App Delegate's method willTerminate is executed?
- Creating a custom UIActivityIndicatorView
- Build fail when using SDWebImage
- Can I have a Swift protocol without functions
- Attack button in SpriteKit
- Run JS Code to fetch data in swift
- iOS: Custom keyboard: I want to send images to the textDocumentProxy(Input controls)
- Trying to override “selected” in UICollectionViewCell Swift for custom selection state
- How do I create a custom iOS view class and instantiate multiple copies of it (in IB)?