How many ways to color grid - combinations

In the 2x2 square grid below, how many ways can I color each region one of 4 colors so that orthogonally adjacent regions are different colors? Assume the grid is fixed in place and cannot be rotated, etc.
(I couldn’t get the picture but it’s just a chessboard, but 2 by 2 instead)
I don’t understand what orthogonally adjacent means. I searched it up but there isn’t like a diagram or a clear explanation of what it is. Also, I know that this problem can be split into cases, but I don’t know how. Can somebody please help me do this problem? Thanks.

Related

How to make people with different distances to the camera appear to be the same sizes?

Now I am doing a project which is called crowd estimation. I am focused on estimating the crowd levels in canteens. My approach is based on subtracting the background and only keeping the people in the foreground. The people are represented by white pixels and the background is black. So I can estimate the crowd level by counting the white pixels in the foreground. However, this needs people with different distances to the camera appear to be the same sizes. Otherwise, the white pixels for the people sitting near to the camera may be 5 times another person who sits far away from the camera. This is not I want, I want their pixels to be almost the same. Below is a screenshot of the canteen:
The first way I have tried is by segmenting the scene into different regions, the near regions are assigned with less weights, the far regions are assigned with more weights. However, the segmentation and weights assigning are all done manually, and it's hard to judge the line to segment and the weights assigned to each region. I use a picture below to show how it is done:
Another way I have tried is the perceptive transform, choosing four points on the input image and mapping them to the 4 points on the output image. However it's hard to choose the 4 points and it's hard to decide whether the people's sizes are the same after the transformation. It's shown below:
Can anyone provide a good way to solve this people's sizes problem(due too different distances)? Your reply is greatly appreciated.

Opencv cv::findchessboardCorners

say we got a 8x9 chessboard, and the function cv::findChessboardCorners recognize it without problem, My question is why does the function not recognize in the same image a a chessboard with smaller size, I tried in a for-loop and decremented the size the function may recognize a chessboard of let say 5x4 and 4x5 but not 6x7 for example ?
any idea why is that happening ?
I already tried debugging the program and I didn't understand what really happens in calibinit.hpp
thanks in advance !
I think the main problem is that you would have ambiguities since it is easily possible to find different smaller chessboards in a larger one.
If you do corner detection on an image consisting of a chessboard, you will find a regular grid of corners.
Then findChessboardCorners needs to find a structur which is very similar to the given chessboard of size (x,y). It will rate the different possibilities to map the chessboard to the regular grid found by the corner detection and these ratings are very similar.
So it is difficult to decide which is THE CHESSBOARD, you are looking for.
It because the recognized board must have light border.

basic idea to implement contour plotting of image

I've been using matplotlib.pyplot.contour to plot the contour of images, and I wonder how to implement the contour plotting, but I found that the code of pyplot.contour hard for me to understand. I have this idea that, for a grayscale image, each pixel has an intensity value, to plot its contour, I might choose a set of intensity values, like partition the range [min-intensity-value, max-intensity-value] to 10 segments [min-intensity-value, val0, val1,..., val8, max-intensity-value], then for each segment's boundary intensity value (like val0, val1,...,val8), find out all those pixels which has the same intensity value, and I think those pixels will form a contour line.
Is my idea a right way to go? Hope anyone can give me a basic idea about how to implement it.
Thanks.
Your idea of connecting all values with the same colour won't work, as there is no guarantee that these are connected. Also, consider e.g. the case of two equal-coloured pixels touching at the corner: There is no way to say whether these are connected or e.g. the other diagonal.
I believe the question is basically what you want to achieve. If you just want to vectorize the image, there are existing tools for that, like e.g. POtrace. If you need something special or have special input data, then you will get better results when you tell people about this. In that case, I would also take an hour or two to look up the very good description for the POtrace algorithm from their website, maybe you can borrow a few good ideas from it?

Counting objects on a grid with OpenCV

I'm relatively new to OpenCV, and I'm working on a project where I need to count the number of objects on a grid. the grid is the background of the image, and there's either an object in each space or there isn't; I need to count the number present, and I don't really know where to start. I've searched here and other places, but can't seem to find what I'm looking for. I will need to be tracking the space numbers of the grid in the future, so I will also eventually need to know whether each grid space is occupied or empty. I'm not going so far as to ask for a coded example, but does anybody know of any source or tutorials to accomplish this task or one similar to it? Thanks for your help!
Further Details: images will come from a stable-mounted camera, objects are of relatively uniform shape, but varying size and color.
I would first answer a few questions:
Will an object be completely enclosed in a grid cell? Or can it be placed on top of a grid line? (In other words, will the object hide a line from the camera?)
Will more than one object be in one cell?
Can an object occupy more than one cell? (closely related to question 1)
Given reasonable answers to those questions, I believe the problem can be broken into two parts: first, identify the centers of each grid space. To count objects, you can then sample that region to see if anything "not background" is there.
You can then assume that a grid space is defined by four strong, regularly-placed, corner features. (For the sake of discussion, I'll assume you've performed the initial image preparation as needed: histogram equalization, gaussian blur for noise reduction, etc.) From there, you might try some of OpenCV's methods for finding corners (Harris corner detector, cvGoodFeaturesToTrack, etc). It's likely that you can borrow some of the techniques found in OpenCV's square finding example (samples/c/square.c). For this task, it's probably sufficient to assume that the grid center is just the centroid of each set of "adjacent" (or sufficiently near) corners.
Alternatively, you might use the Hough transform to identify the principal horizontal and vertical lines in the image. You can then determine the intersection points to identify the extents of each grid cell. This implementation might be more challenging since inferring structure (or adjacency) from "nearby" vertices in order to find a grid center seems more difficult.

Boolean operations on rectangle polygons

Avast there fellow programmers!
I have the following problem:
I have two rectangles overlapping like shown on the picture below.
I want to figure out the polygon consisting of point ABCDEF.
Alternate christmas description: The red cookie cutter is cutting away a bit of the black cookie. I want to calculate the black cookie.
Each rectangle is a data structure with 4 2d-vertices.
What is the best algorithm to achieve this?
This is a special case of general 2D polygon clipping. A good place to start is the Weiler-Atherton algorithm. Wikipedia has a summary and links to the original paper. The algorithm seems to match the data structure you've described pretty well.
Note that it's quite possible you'll end up with a rectangle with a hole in it (if the red one is entirely inside the black one) or even two rectangles (eg if the red is taller and skinnier than the black). If you're certain there is only one corner of the red rectangle inside the black one then the solution should be much simpler.
constructive solid geometry
How precise are the coordinates? If the rectangles are fairly small, the easiest approach might be to just paint them on a canvas, black rectangle first, followed by red. The remaining black pixels on the canvas are the polygon that's left.
Another approach is to split the coordinate grid into a bunch of rectangles based on all of the sides of the rectangles (not counting unbounded rectangles, you have up to 9 rectangles generated if you have two original rectangles). Then just test a representative point from each of these rectangles for membership in the particular polygons to determine which rectangles are in and which are out.
I found some stuff here I might use:
http://www.cgal.org/Manual/3.3/doc_html/cgal_manual/Boolean_set_operations_2/Chapter_main.html
I actually downloaded the CGAL source before I even posted this question, but I think I'll look closer into it.