This one is for a school assignment.
I am new to Processing software and I want to create a rainbow-filled window exactly like in the picture (at the center) below.
The program on the left is the one I have right now.
The program in the center is what I want it to look like.
On the right is the code I am using. I'll copy-paste it here.
void setup() {
size(255, 255);
}
void draw() {
noStroke();
colorMode(RGB, 255,255,255);
for (int i = 0; i <255; i++) {
for (int j = 0; j < 255; j++) {
stroke(j,i,128);
point(i, j);
}
}
}
Any help, suggestions, adjustments to the code would be greatly appreciated. Thanks in advance.
You would benefit from some pseudocode. Never underestimate the power of pseudocode.
In this image, everything you need to do is written plain as day:
Since we're working in RGB, and that the image tells you what to do with red, green and blue, you're already golden, but to make things more transparent we'll alter the code a little bit. Let's forget about the loops for now. Here's what the picture tells you to do:
R -> vertical slider, the closer to the bottom the more red you have
G -> horizontal slider, left is less and right is more
B -> vertical slider, the opposite to the red slider
Now, knowing that your values are on a [0-255] scale, dans that your image also is a 256 pixels wide square, you just have to use the index of your loops to get your RGB values:
for (int i = 0; i <255; i++) {
for (int j = 0; j < 255; j++) {
int r = j; // up == more red
int g = i; // right == more green
int b = 255 - j; // down == less blue
stroke(color(r, g, b));
point(i, j);
}
}
Also, just for kicks, as this is a static image and not an animation, you can put this code in the setup() method and it'll have the same result:
void setup() {
size(255, 255);
for (int i = 0; i <255; i++) {
for (int j = 0; j < 255; j++) {
int r = j; // up == more red
int g = i; // right == more green
int b = 255 - j; // down == less blue
stroke(color(r, g, b));
point(i, j);
}
}
}
void draw() {} // you still need this method even if it's empty
Which gives you this result:
Have fun!
Related
I am trying to fill some particular circle areas on a square grid in Proceesing. However, I am facing some weird filling after I run my code. I used the "atan2" function to get the angle of points in the grid and apply some if conditions to limit the area. But it doesn't work. Actually, it works some way but not exactly what I want.This is the result I run the code-> enter image description here, but it should be like -> enter image description here Can someone help me to solve this, please?
(Additionally, I seized this page to detect which points are in the area I specified enter link description here.
Cell [][] cells;
int res = 10;
PVector circleCenter ;
float rad=290;
void setup() {
size(1200, 800);
cells = new Cell[width/res ][height/res];
for (int i=0; i<cells.length; i++) {
for (int j = 0; j<cells[0].length; j++) {
cells[i][j] = new Cell();
}
}
}
void draw() {
background(255);
circleCenter = new PVector(mouseX, mouseY);
display();
pushStyle();
ellipse(circleCenter.x, circleCenter.y, 20, 20);
popStyle();
//println(frameRate);
}
void display() {
for (int i=0; i<cells.length; i++) {
for (int j = 0; j<cells[0].length; j++) {
if (sq(i*res-width/2) + sq(j*res-height/2) <=sq(rad/2)) {
float angleInRad = atan2(j*res-height/2, i*res-width/2);
///
if (angleInRad<radians(-10) && angleInRad>radians(-60)) {
fill(0, 255, 0); // degrees(atan2(j*res-circleCenter.y, i*res-circleCenter.x))
}
} else {
noFill();
}
rect(i*res, j*res, res, res);
}}
class Cell {
boolean blackness=false;
Cell() {
}
}
You have not specified input data for your task. I assume we have center coordinates cx, cx, radius r, starting and ending angle sa, ea of the sector (in code -10, -60).
There is an approach to check whether vector direction lies in sector, it's robust to potential troubles with periodicity, negative values etc. At first normalize range ends, then find middle angle and half-angle
half = (ea - sa) / 2
mid = (ea + sa) / 2
coshalf = Cos(half)
Now compare that difference of angle and middle one is lower then half-angle
if Cos(angle - mid) >= coshalf then
angle lies in range sa..ea
I want to create 3 images from one bitmap by setting each pixel to the red channel but I keep getting an error with ("java.lang.IllegalStateException") the code that I have now. Original Image
Desired Result
This is the result I'm looking for in these pictures. And then to do the same with the green and blue channels.
I've tried a few things but this is what I have currently:
private Bitmap createRGB(Bitmap r) {
//Red image
for (int x = 0; x < r.getWidth(); x++)
{
for (int y = 0; y < r.getHeight(); y++)
{
r.setPixel(x, y, r.getPixel(x, y) & 0xFFFF0000);
}
}
return r;
}
The original code works seem to be an issue with the scale of the image when going through the loop. The image size of 1200x1100 did not work however, 600x500 produced the correct result. (This is only for my machine, may work for bigger sizes on other android projects)
private Bitmap createRGB(Bitmap r) {
for(int i = 0; i < r.getWidth(); i++){
for(int j = 0; j < r.getHeight(); j++){
r.setPixel(i, j, r.getPixel(i, j) & 0xFFFF0000);
}
}
return r;
}
This works and changing the colour value will also produce Green and Blue results.
I am working on a school project in Processing (Java Mode). We have a picture of how the game should look like.
So the task is to create a grid out of squares. Random squares should light up in red. If a red square is clicked, it should change colors to green and stay green.
What my code looks like at the moment:
Square[][] grid;
int cols = 20;
int rows = 20;
void setup() {
size(400, 400);
grid = new Square[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
grid[i][j] = new Square(i*20, j*20, 20, 20);
}
}
}
void draw() {
background(0);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
grid[i][j].display();
if (grid[i][j].x<mouseX && mouseX < grid[i][j].x + grid[i][j].w && grid[i][j].y<mouseY && mouseY < grid[i][j].y + grid[i][j].h && mousePressed) {
color col = color(0,204,0);
grid[i][j].update(col);
}
}
}
}
Class for squares:
class Square {
float x, y;
float w, h;
color c;
Square(float tempX, float tempY, float tempW, float tempH) {
x = tempX;
y = tempY;
w = tempW;
h = tempH;
c = color(0);
}
void display() {
stroke(0);
fill(c);
rect(x, y, w, h);
}
void update(color c) {
this.c = c;
}
}
So at the moment, every square you click turns green. I am not sure how to write the code, so that random squares change color to red and shuffle every 5 seconds.
Do you have any tips on how to proceed with the code or which thinking steps to take to be able to solve this task?
First, take your task:
So the task is to create a grid out of squares. Random squares should light up in red. If a red square is clicked, it should change colors to green and stay green.
and break it down:
create a grid out of squares: nicely done already !
Random squares should light up in red
If a red square is clicked
change colors to green and stay green
How do you use random numbers in Processing ?
The simplest method is using the random() method: you can pass two values and you'll get back a random number between those values.
Let's say you want to flip a coin so there's a (roughly) 50-50 change you get heads or tails. You could so something like:
if(random(0, 100) > 50){
println("head");
}else{
println("tails");
}
Could even be random(0.0, 1.0) > 0.5 for example, the idea is the same.
You could think of throwing a dice or a number of dices, etc.
Remember these are pseudo-random and in your own time can explore other pseudo random related methods such as randomGauss() and noise().
random() may be good enough for now, part 2 done :)
You're almost done with part 3:
if (grid[i][j].x<mouseX && mouseX < grid[i][j].x + grid[i][j].w && grid[i][j].y<mouseY && mouseY < grid[i][j].y + grid[i][j].h && mousePressed) {
but you need to also check if the clicked square is red.
Would nice to have some red squares to begin with. Let's assume color(204, 0, 0) is your red, you could simply add an additional check:
if(grid[i][j].c == color(204, 0, 0)){
println("red block clicked");
grid[i][j].c = color(0, 204, 0);
}
Which roughly turns your sketch into:
Square[][] grid;
int cols = 20;
int rows = 20;
final color RED = color(204, 0, 0);
final color GREEN = color(0, 204, 0);
void setup() {
size(400, 400);
grid = new Square[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
grid[i][j] = new Square(i*20, j*20, 20, 20);
// roughly 50 - 50 % change a grid square will be red
if (random(0, 100) > 50) {
grid[i][j].update(RED);
}
}
}
}
void draw() {
background(0);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
grid[i][j].display();
if (grid[i][j].x<mouseX && mouseX < grid[i][j].x + grid[i][j].w && grid[i][j].y<mouseY && mouseY < grid[i][j].y + grid[i][j].h && mousePressed) {
// if the square is red
if (grid[i][j].c == RED) {
// change colour to GREEN
grid[i][j].update(GREEN);
}
}
}
}
}
class Square {
float x, y;
float w, h;
color c;
Square(float tempX, float tempY, float tempW, float tempH) {
x = tempX;
y = tempY;
w = tempW;
h = tempH;
c = color(0);
}
void display() {
stroke(0);
fill(c);
rect(x, y, w, h);
}
void update(color c) {
this.c = c;
}
}
In terms of shuffling colours every 5 seconds I recommend:
for every 5 seconds you could use millis()
above there is an example of shuffling done in setup() though you might want to encapsulate a nested loop like that with the random condition in a void shuffle() function for example which you could easily call every 5 seconds.
note that this approach will reset green blocks to red, you might want an else in that condition to reset blocks to black (otherwise, with time, most will turn red), etc.
Have fun!
P.S. I tend to separate state data from representation. For example I would add a variable to keep track of each square state (e.g. OFF, INTERACTIVE, ACTIVATED), update a basic finite state machine, then render colours accordingly. What you have above is a tight coupling between the colour of a Square and it's state. For the homework you've got that's ok, but in the future, for more complex projects you might want to consider data flows through your program and how you represent it.
I want to find with OpenCV first red pixel and cut rest of picture on right of it.
For this moment I wrote this code, but it work very slow:
int firstRedPixel = mat.Cols();
int len = 0;
for (int x = 0; x < mat.Rows(); x++)
{
for (int y = 0; y < mat.Cols(); y++)
{
double[] rgb = mat.Get(x, y);
double r = rgb[0];
double g = rgb[1];
double b = rgb[2];
if ((r > 175) && (r > 2 * g) && (r > 2 * b))
{
if (len == 3)
{
firstRedPixel = y - len;
break;
}
len++;
}
else
{
len = 0;
}
}
}
Any solutions?
You can:
1) find red pixels (see here)
2) get the bounding box of red pixels
3) crop your image
The code is in C++, but it's only OpenCV functions so it should not be difficult to port to Java:
#include <opencv2\opencv.hpp>
int main()
{
cv::Mat3b img = cv::imread("path/to/img");
// Find red pixels
// https://stackoverflow.com/a/32523532/5008845
cv::Mat3b bgr_inv = ~img;
cv::Mat3b hsv_inv;
cv::cvtColor(bgr_inv, hsv_inv, cv::COLOR_BGR2HSV);
cv::Mat1b red_mask;
inRange(hsv_inv, cv::Scalar(90 - 10, 70, 50), cv::Scalar(90 + 10, 255, 255), red_mask); // Cyan is 90
// Get the rect
std::vector<cv::Point> red_points;
cv::findNonZero(red_mask, red_points);
cv::Rect red_area = cv::boundingRect(red_points);
// Show green rectangle on red area
cv::Mat3b out = img.clone();
cv::rectangle(out, red_area, cv::Scalar(0, 255, 0));
// Define the non red area
cv::Rect not_red_area;
not_red_area.x = 0;
not_red_area.y = 0;
not_red_area.width = red_area.x - 1;
not_red_area.height = img.rows;
// Crop away red area
cv::Mat3b result = img(not_red_area);
return 0;
}
This is not the way to work with computer vision. I know this, because I did it the same way.
One way to achieve your goal would be to use template matching with a red bar that you cut out of your image, and thus locate the red border, and cut it away.
Another would be to transfer to HSV space, filter out red content, and use contour finding to locate a large red structure, as you need it.
There are plenty of ways to do this. Looping yourself over pixel-values rarely is the right approach though, and you won't take advantage of sophisticated vectorisation or algorithms that way.
I am doing OCR and sometime I have a light text on a dark background. When I encounter this situation I need the program to know that it has to invert the colours. The code I have wrote doesn't work how I want. It is detecting dark colours as light and light colours as dark. Any ideas what I've done wrong?
File input = new File("/Users/unknown1/Desktop/t5.png");
BufferedImage imagegrey = ImageIO.read(input);
toGray(imagegrey);
int width = imagegrey.getWidth();
int height = imagegrey.getHeight();
int light = 0;
int dark = 0;
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
Color color = robot.getPixelColor(j, i);
int grey = ((color.getRed() + color.getBlue() + color.getGreen())/3);
//System.out.println(grey);
if (grey >= 237) {
light++;
}
else {
dark++;
}
}
}
System.out.println(light);
System.out.println(dark);
This as itself should work - pick up the color at location j i on the screen. Now if that location is covered by the image you intend is another issue we [here] dont' know what is in that spot. And if toGray() does some wierd transform that negates the image. So check these
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
Color color = robot.getPixelColor(j, i);
int grey = ((color.getRed() + color.getBlue() + color.getGreen())/3);
//System.out.println(grey);
if (grey >= 237) {
light++;
}
else {
dark++;
}
}