Corona sdk linear color ramp? - lua

Ok, I have a very, very large background image, well not an image but a rectangle colored blue:
bg2 =display.newRect(0,0,20000,20000)
bg2.y=10000
bg2:setFillColor( 0 , 0, 225)
Is it possible to make the rectangle not just one solid color, but a linear color ramp from black to blue? In other words, the color fades from black to blue vertically. I don't want to use an image, because it would be way too large.

You're looking for a gradient fill color, such as this.
local black = {0, 0, 0}
local blue = {0, 0, 1}
local g = {type="gradient", color1=black, color2=blue}
bg2:setFillColor(g)

Related

how to conditionally apply threshold in opencv

I have 2 types of image to deal. one with white background and another type with dark background. My requirement is to apply different thresholds for each type
for ex : for white back ground
(thresh, img_bin) = cv2.threshold(img, 128 , 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
for dark back ground
(thresh, img_bin) = cv2.threshold(img, 128 , 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
I am reading images using cv.imread(img,0)
I am doing morphological transformation , so i need to invert the white back ground image. but for the dark background i don't want to invert.
To expand on #nathancy's comment, you could use one of the OpenCV functions, sum or mean
For an plain old 24 bit color image, black and white are represented by
( 0, 0, 0) black
(255, 255, 255) white
Here is a sample image that looks like a page out of a book:
Now let's run some code on it
import cv2 as cv
import numpy as np
img = cv.imread('lorem_ipsum.png',cv.IMREAD_COLOR)
ret = cv.mean(img)
print(ret)
ret = cv.mean(ret)
print(ret)
ret = ret*4/3
print(ret)
ret = cv.mean(cv.mean(img))[0]*4/3
print(ret)
which gives output:
(229.78, 228.28, 228.95, 0.0)
(171.74, 0.0, 0.0, 0.0)
228.98
228.98
The first line gives us the mean of blue, green, red, and alpha channels. Second line is mean of the means. Because the first line had a zero entry, the mean of means is too low. We want to ignore alpha channel. So on the last line we pick off just the first element in mean of means and scale it by 4/3 to get a 0 to 255 answer. Our answer is 228.98 --> the picture is mostly white. The last line is the result of doing all of the operations in one line.

How to increment yellow pixels in RGB image

I have an image stored in RGB color space and I need to detect yellow pixel and increment each one by 5.
For example, if I have a photo with a yellow lemon and a brown table, I have to turn the lemon more yellow and the table must remain the same.
Then I have to save the new image.
How can I perform it with openCV and C++?
Yes.
Convert image into HSV color space.
Calculate yellow range in HSV (from Scalar to Scalar).
Create binary mask for yellow: inRange.
Call add with mask from (3) for your HSV image and cv::Scalar(5, 0, 0)
Convert Result to RGB.
Example:
cv::Mat rgbImg = cv::imread("src.jpg", cv::IMREAD_COLOR);
cv::Mat hsvImg;
cv::cvtColor(rgbImg, hsvImg, cv::COLOR_BGR2HSV);
cv::Mat threshImg;
cv::inRange(hsvImg, cv::Scalar(20, 100, 100), cv::Scalar(30, 255, 255), threshImg);
cv::imwrite("thresh.png", threshImg);
cv::add(hsvImg, cv::Scalar(5, 0, 0), hsvImg, threshImg);
cv::cvtColor(hsvImg, rgbImg, cv::COLOR_HSV2BGR);
cv::imwrite("res.png", rgbImg);
And pictures:

Define black region in HSV color space

I want to find boundaries of black region
http://i40.tinypic.com/2lbi9s9.jpg
http://i44.tinypic.com/ka4vuc.jpg
I tried different values for black, but coluld find average value so region is thresholded in both pictures
One of ranges is
inRange(src_HSV, Scalar(0, 0, 0), Scalar(180, 150, 50), src_HSV);
Another is
inRange(src_HSV, Scalar(100, 40, 140), Scalar(140, 160, 255), src_HSV);
I tried to search the Internet for values of black, but couldn't find anything suitable for this case, having different tones of black
Note that in HSV, black is defined as V=0, independently of H and S (in your case, you probably need to look for small values of V and S). I would ignore the H component.
inRange(src_HSV, Scalar(0, 0, 0), Scalar(179,50, 100), src_HSV);
for black and grey shades.
Anyways it is application specific.
Follow this link to get good insights on HSV:
Would this be an option (in RGB):
if ((red + green + blue) <= 64) {
// black
} else {
// not black
}
If not you could try HSL (hue, saturation, lightness) values and set black if lightness < 10% ...

How to print a colored text on white background with OpenCV

Can I print a colored foreground on white background text with printext in OpenCV.
I tried with:
putText(frame, "Male", centerPrint,FONT_HERSHEY_COMPLEX_SMALL, 0.8,cvScalar(255, 0, 0), 1, CV_AA);
But it prints a blue text on transparent background.
Thank you
colors are bgr, not rgb ( so red is (0,0,255)). and you can't change the background.( unless you draw a filled rect with bg color there before)

CGContextStrokePath color

I use CGContextStrokePath painted on a straight line in a white background picture, stroke color is red, alpha is 1.0
After drawing the line, why the points is not (255, 0, 0), but (255, 96, 96)
Why not pure red?
Quartz (the iOS drawing layer) uses antialiasing to make things look smooth. That's why you're seeing non-pure-red pixels.
If you stroke a line of width 1.0 and you want only pure red pixels, the line needs to be horizontal or vertical and it needs to run along the center of the pixels, like this:
CGContextMoveToPoint(gc, 0, 10.5);
CGContextAddLineToPoint(gc, 50, 10.5);
CGContextStroke(gc);
The .5 in the y coordinates puts the long along the centers of the pixels.

Resources