What's the most efficient way to set the color of a TextView and why? Is there 1 method that is more memory and/or processor efficient? Or is there no difference at all to what happens with my app when it's running? Is it better to refer to a colour resource than declare RGB everytime I set a colour?
Option 1 (using RGB channel)
myTextView.setTextColor(Color.rgb(154,160,166))
Option 2 (using colour parser)
myTextView.setTextColor(Color.parseColor("#2B3A11"))
Option 3 (using colour resource from colors.xml)
myTextView.setTextColor(ContextCompat.getColor(context, R.color.<name_of_colour>))
Option 1 Should be the fastest way to set color to your TextView. With option 2 being a very closed Second. Because both RGB values and Hexcodes function in a similar way. It all comes down to the functions they are called from and how these functions are executed in the background (look rgb parseColor for these function's description and implementation ).
As
myTextView.setTextColor(...) is the same. So what happens to your textview in the background remains the same only the picking up of color is done differently.
Is it better to refer to a color resource than declare RGB everytime I set a color?
Ans- It depends on the your own usage,for example if you want to use a color multiple times and find it harder to remember the rgb code then you should definitely save the color int the color resource and refer to it later. The color resource was created for this purpose only right! And as #Fancesc mentioned it does make your documents readable and maintainable. Hence, the more professional way of doing things.
On the other hand using rgb right whenever needed saves you a lot of trouble.
You don't need to save any data anywhere.
While using you don't need to search the whole color resource file.
99% times the color model used is rgb/rgba, so everything you do ends up here.
Plus newer updates make it easier to work on rgb and hex values for colors. one such example is shown here.
Good for people who are bad with color names.
I am assuming you knew all of this already but decided to ask it anyways.XD. Have a good Day.
Related
Both methods are not documented both and does not seem to behave as I would expect.
mylabel.setFontScale(3f); makes the apparent text 3 times larger (what I'm looking for) but does not center properly when using it with Align.center.
mylabel.setScale(3f); does nothing as far as I could see.
What is the difference between those 2 methods and what one should I use to make my label bigger and properly centered ?
setFontScale() indeed enlarges the font, this is often unwanted since scaling up pixelizes the font.
label.setScale() does work if you are not using a layout actor like a table. When actors go inside a layout then the layout is responsible for setting the sizes and position. For example, you have control over this once you put it inside a table cell.
table.add(myLabel).width(300).height(80).padLeft(100).expandX();
Detailed documentation on working with a table.
This however just sizes the label, not the text inside. You can still do setFontScale(3f) but this will pixelate the font. You have 2 better options:
create a extra bitmap font in Hiero 3 times larger.
Use Gdx.Freetype to import a .ttf and generate fonts on run-time.
My aim is to let the user take a selfie from my app and then apply different image processing algorithms to convert it in a cartoon type image. I followed the algo written here, and then also used the method written just below the chosen answer to convert black and white sketch to colored image that should look like cartoon. Every thing is ok except that after applying Gaussian Blur , the image becomes too hazy and unclear. Here is the image output:
Any advice how I can make it more clear? Like shown in this link. I know they used Photoshop , but I want to achieve it with Java and Android.
PS: I found all the image processing methods from here. Plus the method mentioned here (the one below the chosen answer), what could be the ideal values in the arguments?
cartoonifying image
If you have a basic knowledge of C++, you can port this app for your need.
This application works real time. If you want to non-real time,than you can use bilateral filter against two medianBlurs at the bottom of function. That will give better results, but bad performance. But you need to use OpenCV in your application. If you want to make application with more functions, I will suggest you to do it.
I have one method to combine 3 greyscale images to one colour image which is done by using getRed(), getGreen() and getBlue() in Java, for each individual input image and then applying the colour to the output image which works quite well. Im looking to find other methods for doing this however.
It doesnt have to be accurate in terms of sea being blue, etc. but it needs to be coloured in a way that different areas of the 'map' can be differentiated.
Ive been looking into ways of doing this but unfortunately havent actually managed to find an alternative way of doing it, im looking to use something apart from the getRGB() values.
Im not looking for anyone to code for me, just to give me some pointers on what to look for.
Thanks!
Your comment here is critical: "but it needs to be coloured in a way that different areas of the 'map' can be differentiated. Ive been looking into ways of doing this but unfortunately havent actually managed to find an alternative way of doing it"
What did you do with the other 4 images - or "channels". Usually, when doing color space mapping one has 3 channels and one converts to another 3 channel color space. In your case you have 7 channels, and you want to put all that information into 3 channels? It all depends on what you are viewing. Hyperspectral imagery would be a good place to start to see containers for storing imagery data with more than 3 channels.
You can convert to a different colorspace as others have suggested or perform any other transformation. It sounds like though in order to differentiate different parts of the image, you will need some post processing. This will depend on your transformation.
I'm trying to figure out a good method for comparing two images in terms of their color. One idea I had was to take the average color of both images and subtract that amount to get a "color distance." Whichever two images have the smallest color distance would be a match. Does this seem like a viable option for identifying an image from a database of images?
Ideally I would like to use this to identify playing cards put through an image scanner.
For example if I were to scan a real version of this card onto my computer I would want to be able to compare that with all the images in my database to find the closest one.
Update:
I forgot to mention the challenges involved in my specific problem.
The scanned image of the card and the original image of the card are most likely going to be different sizes (in terms of width and height).
I need to make this as efficient as possible. I plan on using this to scan/identify hundreds of cards at a time. I figured that finding (and storing) a single average color value for each image would be far more efficient than comparing the individual pixels of each image in the database (the database has well over 10,000 images) for each scanned card that needed to be identified. The reason why I was asking about this was to see if anyone had tried to compare average color values before as a means of image recognition. I have a feeling it might not work as I envision due to issues with both color value precision and accuracy.
Update 2:
Here's an example of what I was envisioning.
Image to be identified = A
Images in database = { D1, D2 }
average color of image A = avg(A) = #8ba489
average color of images in database = { #58727a, #8ba489 }
D2 matches with image A because #8ba489 - #8ba489 is less than #8ba489 - #58727a.
Of course the test image would not be an exact match with any of those images because it would be scanned in; however, I'm trying to find the closest match.
Content based image retrieval (CBIR) can do the trick for you. There's LIRE, a java library for that. You can even first try several approaches using different color based image features with the demo. See https://code.google.com/p/lire/ for downloads & source. There's also the "Simple Application" which gets you started with indexing and search really fast.
Based on my experience I'd recommend to use either the ColorLayout feature (if the images are not rotated), the OpponentHistogram, or the AutoColorCorrelogram. The CEDD feature might also yield good results, and it's the smallest with ~ 60 bytes of data per image.
If you want to check color difference like this:
http://en.wikipedia.org/wiki/Color_difference
You can use Catalano Framework,
http://code.google.com/p/catalano-framework/
It works in Java and Android.
Example using Color Difference:
float[] lab = ColorConverter.RGBtoLAB(100, 120, 150, ColorConverter.CIE2_D65);
float[] lab2 = ColorConverter.RGBtoLAB(50, 80, 140, ColorConverter.CIE2_D65);
double diff = ColorDifference.DeltaC(lab, lab2);
I think your idea is not good enough to do the task.
Your method will say all images below are the same (average color of all images are 128).
Your color averaging approach would most likely fail, as #Heejin already explained.
You can do try it in different way. Shrink all images to some arbitrary size, and then subtract uknown image from all know images, the one with smallest difference is the one you are looking for. It's really simple method and it would't be slower than the averaging.
Another option is to use some smarter algorithm:
http://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html
I have used this method in past and the results are okay-ish. Ir works great for finding same images, not so well for finding siilar images.
I would like to turn everyone's attention to this question before I begin as I want to do something similar: Getting the most common color of an image
Now, I have tested this and it is effective by filtering out the greys although does it really find us the most significant colours? It finds the most yes, although some greys do still show up.
I personally don't feel its the most efficient way. I have a colour histogram function, for both RGB and HSB and this divide up the space, but where do you go from here
If you want to find the most significant color you can do the following:
Reduce number of colors in image. See this discussion - algorithm is the same.
Cycle throught image and count each color.
Find maximum of that array of colors. This will be your color.