How to implement the conditional probability for each pixel? - matlab

I have asked a 'very broad' question yesterday link about build conditional random field based energy function from images. I got negative feedback from the comments and I think I should modify the question and make it more specific.
Here I have got bunch of images with a cow on the grass and some sky at the background. I want to segment the cow from the grass and sky (a toy problem only).
I firstly over-segment the images using some super-pixel method, and I have got the ground truth of labels of my 10 training images. Here is an example,
Then I pass these super-pixel patches to some filter to get texture features and saved them into feature vectors for cow (c), grass (g) and sky (s)
My question is how to using Matlab to implement the conditional probabilities of these three classes?
Like P(X_i|C_c), P(X_i|C_g) and P(X_i|C_s)? X_i are each super-pixel, C_x are the three classes.
I think some webpage mentioned about using Matlab hist function. Not sure how and why to do that. Please give me some basic applicable hints not complicated papers. Thanks a lot. A.

Related

How to use perlin noise for 2D Tile map in top down game in Unity?

As the title suggests I want to use perlin noise to make a randomly generated terrain using tiles that I have made with a pixel width of 32x32, I've had a little look at perlin noise but it seems very confusing and it would be great if anyone could point me into a good direction
I'll give some details into what i want to add into the game if that is needed
it will be a sort of like Minecraft game but topdown 2d, it will have the normal stuff like grass, water, sand, dirt, trees, bushes, rocks, forests, maybe already made structures? like towns or something but that would probably be used with a different type of noise I would think
Hopefully all that helps and thank you for any help you can give!
I'll simplify your case to a black/white example where your map only consists of two types of tiles, e.g. grass and sand.
Now you can go over your map and determine for every tile if it should be sand or grass by sampling the noise function at the position of the tile; if your noise creates values in the range [0, 1] you could just split it in the middle and say every tile with a value below .5 is sand, every other tile is grass.
Now a next step to consider is how to handle edges. There are two possibilities to take here.
Addendum: I think Unity has out-of-the-box solutions for this, but in case you want or have to implement it yourself:
1. Option: Have tiles for every neighbour-combination: If you look at this tileset from Pokemon, you see in the top left corner a sand tile. This can be used in case all eight neighbours are also sand. Next to it is a tile with a grass edge on the left. This can be used for the case that all three left neighbours are grass, like this:
🟩🟨🟨
🟩❌🟨
🟩🟨🟨
And that pattern goes on. you basically have a tile option for every 8² possible neighbour-combinations. (Note that Pokemon does not have all combinations).
Also notable here is that you have a clear 'background'-tile. In Pokemons case the grass. The edges are drawn onto the sand tiles.
2. Option: Have tiles for in between your sampled points. This is called Wang-Tiles, more details here.
Adding more than two types of tiles
If you want more than two types of tiles you have also multiple options.
1. Option: Multiple tile types from one noise function. You could sample your noise function kind of like a heightmap and say everything below .2 is water, everything between .2 and .3 is sand, between .3 and .8 is grass and above .8 is snow. This just uses one noise function, so it is rather simple, but you will likely recognize the layers (meaning snow will never be generated next to sand or water).
2. Option: A multi-noise biome system (like minecraft does if I'm not mistaken). You can e.g. use two separate noise functions to sample temperature and precipitation (again, like minecraft if I'm not mistaken) and then look up the value on a chart to determine the biome (which can determine the ground tile type). Here is an example of such a biome map:
3. Option: This is just a placeholder to tell you that there are other (infinite) ways to determine the floor texture for your game. Have a look at my collection of resources, I'm sure you'll find something there.
Mix your methods!
In the end what you probably want to go for is complex logic to determine your ground type. Mix and match the methods you find. E.g. use some biome map, but additionally a height map to determine where water is. Or add another noise layer that determines corruption from an evil force. Or you have a separate noise functions for vegetation. The possibilities are endless.

Using a neural network with genetic algorithm for pong or supermario

I'm trying to use GA to train an ANN whose job is to move a bar vertically so that it makes a ball bounce without hitting the wall behind the bar, in other words, a single bar pong.
I'm going to ask it directly because i think to know what the problem is.
The game window is 200x200 pixels, so i created 40000 input neurons.
The obvious doubt is: can GA handle chromosomes of 40000(input)*10(hidden)*2 elements(genes)?
Since i think the answer is no(i implemented this solution and doesn't seem to work), the solution seems simple, i feed the NN with only 4 parameters which are the coordinates x,y of bar and ball, nailed it.
Nice solution, but the problem is: how can i apply such a solution in a game like supermario where the number of enemies in the screen is not fixed? Surely i cannot create a NN with dynamic numbers of inputs.
I hope you can help me.
You have to use features to represent your state. For example, you can divide the screen in tiles and assign a value according to a function that takes into account the enemy (e.g., a boolean if the enemy is in the tile or the distance to the closest enemy).
You can still use pixels but you might need to preprocess them in order to reduce their size (e.g., use a recurrent NN).
Btw, a NN might not be able to handle 200x200 pixels, but it was able to learn to play Atari games using a representation of the state by preprocessed pixels of size 84x84x4 (see this paper).

Bald detection using image processing

I was wondering if someone can provide me a guideline to detect if a person in a picture is bald or not, or even better, how much hair s\he has.
So far I tried to detect the face and the eyes position. From that information, I roughly estimate the forehead and bald area by cutting the area above the eyes as high as some portion of the face.
Then I extract HOG features and train the system with bald and not-bald images using SVM.
Now when I'm looking at the test results, I see some pictures classified as bald but some of them actually have blonde hair or long forehead that hair is not visible after the cutting process. I'm using MATLAB for these operations.
So I know the method seems to be a bit naive, but can you suggest a way of finding out the bald area or extracting the hair, if exists. What method would be the most appropriate for that kind of problem?
very general, so answer is general unless further info provided
Use Computer Vision (e.g MATLAB Computer Vision toolkit) to detect face/head
head has analogies (for human faces), using these one can get the area of the head where hair or baldness is (it seems you already have these)
Calculate the (probabilistic color space model) range where the skin of the person lies (most peorple have similar skin collor space range)
Calculate percentage of skin versus other color (meaning hair) in that area
You have it!
To estimate a skin color model check following papers:
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.56.8637&rep=rep1&type=pdf
http://infoscience.epfl.ch/record/135966
http://www.eurasip.org/Proceedings/Eusipco/Eusipco2010/Contents/papers/1569293757.pdf
Link
If an area does not fit well with skin model it can be taken as non-skin (meaning hair, assuming no hats etc are present in samples)
Head region is very small, hence, using HOG for classification doesn't make much sense.
You can use prior information - like detect faces; baldness/hair is certain to be found on the area above the face. Also, use some denser feature descriptors.
You are probably ending up with very sparse representation or equivalently less information because of which your classifier is not able to classify correctly.

Biological Cell shape detection with Matlab

I have a problem with shape detection on Matlab. I got two types of circular cell shapes but one is erythrocyte that has little difference to another cell that is leukocyte and is also circular. How could I distinguish them from each other with image processing?
Maybe will parent-child relationship be useful to detect circle in erythrocyte? Or other techniques?
There are 4 types of cell detection/segmentation: pixel-based, region-based, edge-based and contour-based segmentation. You may use one or several combinations of them for your task. But counting only on the shape may be insufficient.
The main difference between erythrocyte and leukocyte is the existence of nucleus. To my knowledge, the nucleus staining is often applied to microscopy. If that is the case,
(i) the ratio between green and blue channel intensities of each pixel can be used as discriminating feature to separate the nucleus pixels from other foreground pixels;
(ii) After that, it is possible to extract the leukocyte plasma based on the hue-value similarity between the pixel from that region and the nucleus region;
(iii) Contour-based methods such as active-contour methods (snakes) and level-set approaches can be used to refine the boundaries of white blood cells;
(iv) What left to you after (i)-(iii) are probably the erythrocytes. If your task also includes the segmentation of erythrocytes, you may threshold them easily (or search the studies for more accurate segmentation algorithms).
I would recommend T.Bergen et al, Segmentation of leukocytes and erythrocytes in blood smear images. My description above was included and detailed in this paper, and they applied more sophisticated strategies to improve the boundary accuracy. You may try to follow their steps and reproduce the similar result if your ultimate goal is also segmentation. Yet only detection without extraction might be much easier.

What are the features in feature detection algorithms and other doubts

I am going through feature detection algorithms and a lot of things seems to be unclear. The original paper is quite complicated to understand for beginners in image processing. Shall be glad if these are answered
What are the features which are being detected by SURF and SIFT?
Is it necessary that these have to be computed on gray scale images?
What does the term "descriptor" mean in simple words.
Generally,how many features are selected/extracted?Is there a criteria for that?
What does the size of Hessian matrix determine?
What is the size of the features being detected?It is said that the size of a feature is the size of the blob.So, if size of image is M*N so will there be M*N n umber of features?
These questions may seem too trivial, but please help..
I will try to give an intuitive answer to some of your questions, I don't know answers to all.
(You didn't specify which paper you are reading)
What are the features and how many features are being detected by SURF
and SIFT?
Normally features are any part in an image around which you selected a small block. You move that block by a small distance in all directions. If you find considerable variations between the one you selected and its surroundings, it is considered as a feature. Suppose you moved your camera a little bit to take the image, still you will detect this feature. That is their importance. Normally best example of such a feature is corners in the image. Even edges are not so good features. When you move your block along the edge lines, you don't find any variation, right?
Check this image to understand what I said , only at the corner you get considerable variation while moving the patches, in other two cases you won't get much.
Image link : http://www.mathworks.in/help/images/analyzing-images.html
A very good explanation is given here : http://aishack.in/tutorials/features-what-are-they/
This the basic idea and the algorithms you mentioned make this more robust to several variations and solve many issues. (You can refer their papers for more details)
Is it necessary that these have to be computed on gray scale images?
I think so. Anyway OpenCV works on grayscale images
What does the term "descriptor" mean in simple words?
Suppose you found features in one image, say image of a building. Now you took another image of same building but from a slightly different direction. You found features in the second image also. But how can you match these features. Say feature 1 in image 1 match to which feature in image 2 ? (As a human, you can do easily, right ? This corner of building in first image corresponds to this corner in second image, so and so. Very easy).
Feature is just giving you pixel location. You need more information about that point to match it with others. So you have to describe the feature. And this description is called "descriptors". To describe this features, algorithms are there and you can see it SIFT paper.
Check this link also : http://aishack.in/tutorials/sift-scale-invariant-feature-transform-introduction/
Generally,how many features are selected/extracted?Is there a criteria
for that?
During processing you can see applying different thresholds, removing weak keypoints etc. It is all part of plan. You need to understand algorithm to understand these things. Yes, you can specify these threshold and other parameters (in OpenCV) or you can leave it as default. If you check for SIFT in OpenCV docs, you can see function parameters to specify number of features, number of octave layers, edge threshold etc.
What does the size of Hessian matrix determine?
That I don't know exactly, just it is a threshold for keypoint detector. Check OpenCV docs : http://docs.opencv.org/modules/nonfree/doc/feature_detection.html#double%20hessianThreshold