image segmentation based on colors using k means, opencv [closed] - k-means

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to
1) Read a 3 channel image
2) Using K-Means method, create k different classes
3) I will tag pixels accordingly to show which class they belong to and store them in a matrix.
4) And after that I'm thinking using Connected Component Labeling to determine if they are in the same segment or not.
I'm new in opencv so I wanted to ask you for the implementation , some code snippet to to get me started.
Any help will be appreciated. Thank you

typedef struct {
int red;
int green;
int blue;
int b64;
int groupNo;
} IMAGE;
typedef struct _MEANS
{
int r, g, b;
int groupNo;
} point_t, *point;
Mat img = imread("C:\\253027.jpg",CV_LOAD_IMAGE_COLOR);
int clusterCount=5;
IMAGE **strct= {0};
strct = allocate_matrix(img.rows, img.cols);
int i,j,k;
if(! img.data )
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
for ( i = 0; i < img.rows; i++) {
for ( j = 0; j < img.cols; j++) {
strct[i][j].red = img.at<Vec3b>(i,j)[2];
strct[i][j].green = img.at<Vec3b>(i,j)[1];
strct[i][j].blue = img.at<Vec3b>(i,j)[0];
/* printf("%u",strct[i][j].red);
printf("%u",strct[i][j].green);
printf("%u",strct[i][j].blue);
*/
}
}
double *tempuz= (double *)malloc(sizeof(double) * clusterCount);
point_t* p = (point_t *)malloc(sizeof(point_t) * clusterCount);
for( k = 0; k < clusterCount; k++ )
{
p[k].r= rand()%255;
p[k].g=rand()%255;
p[k].b=rand()%255;
p[k].groupNo=k;
}
int max=0;
for (i = 0; i < img.rows; i++) {
for ( j = 0; j < img.cols; j++) {
for(k = 0; k < clusterCount; k++ ){
tempuz[k]= abs(strct[i][j].red - p[k].r)+abs(strct[i][j].green -p[k].g)+abs(strct[i][j].blue - p[k].b);
// cout<<"tempuz"<<tempuz[k]<<endl;
} strct[i][j].groupNo=min_element(tempuz,clusterCount);
// cout<<"grup no"<<strct[i][j].groupNo<<endl;
}
}
i=0;
int r=0,b=0,g=0,counter=0;
while(i<200){
r=0,b=0,g=0,counter=0;
for(k=0;k<clusterCount;k++){
for (i = 0; i < img.rows; i++) {
for ( j = 0; j < img.cols; j++) {
if(strct[i][j].groupNo==k){
r+=strct[i][j].red;
b+=strct[i][j].blue;
g+=strct[i][j].green;
counter++;
}
}
}
if(counter!=0){
p[k].r=r/counter;
p[k].b=b/counter;
p[k].g=g/=counter;
}
}
i++;
max=0;
for (i = 0; i < img.rows; i++) {
for ( j = 0; j < img.cols; j++) {
for(k = 0; k < clusterCount; k++ ){
tempuz[k]= abs(strct[i][j].red - p[k].r)+abs(strct[i][j].green -p[k].g)+abs(strct[i][j].blue - p[k].b);
} strct[i][j].groupNo=min_element(tempuz,clusterCount);
}
}
}//end of while
for(int k=0;k<clusterCount;k++){
for (int i = 0; i < img.rows; i++) {
for (int j = 0; j < img.cols; j++) {
if(strct[i][j].groupNo==k){
strct[i][j].red=p[k].r;
strct[i][j].blue=p[k].b;
strct[i][j].green=p[k].r;
}
}
}
}
for (int i = 0; i < img.rows; i++) {
for (int j = 0; j < img.cols; j++) {
img.at<Vec3b>(i,j)[2] = strct[i][j].red;
img.at<Vec3b>(i,j)[1] = strct[i][j].green;
img.at<Vec3b>(i,j)[0] = strct[i][j].blue;
}
}

Related

How to access the values of a dictionary property in a grid using Ocean for Petrel?

I'm tring to access the values of a dictionary property in a grid,such as Fluvial facies or lithologies etc.I have read the coursebook and help docs, but didn't find anything relevant.The coursebook only has examples of creating properties, but not accessing properties.Below is the code I tried:
Grid grid = arguments.Input_Grid;
if (grid == null)
{
PetrelLogger.ErrorStatus("HelloGrid: Arguments cannot be empty.");
return;
}
Index3 currentCell = new Index3();
int maxI = grid.NumCellsIJK.I;
int maxJ = grid.NumCellsIJK.J;
int maxK = grid.NumCellsIJK.K;
for (int i = 0; i < maxI; i++)
{
for (int j = 0; j < maxJ; j++)
{
for (int k = 0; k < maxK; k++)
{
currentCell.I = i; currentCell.J = j; currentCell.K = k;
if (grid.IsCellDefined(currentCell) && grid.HasCellVolume(currentCell))
{
//DictionaryProperty p = ???
//int val = p[currentCell] ???
}
}
}
}
You need to use the "FastDictionaryPropertyIndexer" or "FastPropertyIndexer" for regular properties.
foreach (var dictProp in grid.DictionaryProperties)
{
int numCellsI = dictProp.NumCellsIJK[0];
int numCellsJ = dictProp.NumCellsIJK[1];
int numCellsK = dictProp.NumCellsIJK[2];
float[] values = new float[dictProp.NumCells];
var dpsa = dictProp.SpecializedAccess;
using (var fdpi = dpsa.OpenFastDictionaryPropertyIndexer())
{
int index = 0;
for (int k = 0; k < numCellsK; k++)
{
for (int j = 0; j < numCellsJ; j++)
{
for (int i = 0; i < numCellsI; i++)
{
values[index] = fdpi[i, j, k];
index++;
}
}
}
}
}
You also need to be careful about the indexing since it varies by project. For instance, you may need to reverse the order of traversal in the J direction or you could end up with some strange results.

Did i calculate the Big O for these functions correctly?

I tried to find the time complexity of the following two functions:
the first one
public static int myMethod1(int[] arr) {
int x = 0;
for (int i = 0; i < arr.length / 2; i++) {
for (int j = 0; j < arr.length; j++) {
for (int k = 0; k < arr.length; k++) {
x++;
if (k == arr.length / 2) {
break;
}
}
}
}
return x;
}
So with this one i am thinking.
The method contains 3 loops, and the loops are iterating over variable i, j and k…
i and j, and k are both incremented by 1 for each passing… this gives us as N For each LOOP which leaves us with three N’s.., which gives is O(N^3)
The next one is:
public static int myMethod(int N) {
int x = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N / 2; j++) {
for (int k = 1; k < N;) {
x++;
k *= 2;
}
}
}
return x;
}
With this i am thinking.
The method contains 3 loops, and the loops are iterating over variable i, j and k… i and j are both incremented by 1 for each passing… this gives us as N For each LOOP which leaves us with two N’s.. The last loop k doubles, which gives is log(n).
The result of the this problem is therefore O(N^2· log (N))
is this correct? and if it is not, why?
You are right. In both of the questions

How I can sort an ArrayList of int arrays?

I have this code. I'm dealing with the N-Queen problem.
The problem is when I wanna show results by screen, the arrays are not ordered. But in this code I can't order them using Comparator. It's very strange because in other Class it works perfectly using Comparator, but here it doesn't work. Hope anyone could help me. Thanks in advance.
import java.util.*;
public class NReinas {
public static void resolverReinas(int n){
String[][] tablero;
tablero = generarTablero(n);
ubicarReina(tablero, 0, n);
}
public static void ubicarReina(String[][] tablero, int etapa, int n){
ArrayList <int[]> resultados = new ArrayList<>();
for(int i = 0; i < tablero.length; i++){
if(isValido(tablero, i, etapa)){
tablero[i][etapa] = "R";
if(etapa < tablero.length - 1){
ubicarReina(tablero, etapa + 1, n); //Recursividad
}else {
resultados.add(devolverSolucion(tablero, n));
}
tablero[i][etapa] = " "; //Backtracking: vaciamos el tablero
}
}
//The ArrayList I want to order by int arrays
for (int[] r : resultados) {
System.out.println(Arrays.toString(r));
}
}
public static boolean isValido(String[][] tablero, int i, int etapa){
for(int x = 0; x < etapa; x++){
if(tablero[i][x].equals("R")){
return false;
}
}
for(int j = 0; j < tablero.length && (i-j) >= 0 && (etapa-j) >=0; j++){
if(tablero[i - j][etapa - j].equals("R")){
return false;
}
}
for(int j = 0; j < tablero.length && (i + j) < tablero.length && etapa - j >= 0; j++){
if(tablero[i + j][etapa - j].equals("R")){
return false;
}
}
return true;
}
public static String[][] generarTablero(int length){
String[][]res = new String[length][length];
for (int i = 0; i < res.length; i++) {
for (int j = 0; j < res.length; j++) {
res[i][j] = " ";
}
}
return res;
}
public static int[] devolverSolucion(String[][] tablero, int n){
int[] solucion = new int[n];
for (int i = 0; i < tablero.length; i++) {
for (int j = 0; j < tablero.length; j++) {
if(tablero[i][j] == "R"){
solucion[i] = j;
}
}
}
return solucion;
}
}
Try Using Integer instead of int and save array values on List instead, so you can use sort them
List<Integer> list = Arrays.asList(solucion);
Collections.sort(list);
If you insist in using and array you can reconverti the list to an array
(Integer[]) list.toArray();

TLE in Foe Pairs (Educational Codeforces Round 10)

On implementing O(N+M) complexity code for Foe Pairs problem
http://codeforces.com/contest/652/problem/C, I am getting TLE in Test Case 12.
Constraint : (1 ≤ N, M ≤ 3·105)
I am not getting, why for this constraint O(N+M) is getting TLE.
Here, is the code
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
std::vector<int> v(n+1);
for (int i = 0; i < n; ++i)
{
int x;
cin>>x;
v[x] = i;
}
std::vector<int> dp(n,0);
for (int i = 0; i < m; ++i)
{
int a,b;
cin>>a>>b;
if(v[a]>v[b])
swap(a,b);
dp[v[b]] = max(dp[v[b]], v[a]+1);
}
for (int i = 1; i < n; ++i)
{
dp[i] = max(dp[i], dp[i-1]);
}
long long s = 0;
for (int i = 0; i < n; ++i)
{
s+=(i+1-dp[i]);
}
cout<<s;
}
Is there anything, I am missing?
I changed all cin to scanf, it passed all test cases : http://codeforces.com/contest/652/submission/17014495
#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n,m;
scanf("%d%d", &n, &m);
//cin>>n>>m;
std::vector<int> v(n+1);
for (int i = 0; i < n; ++i)
{
int x;
//cin>>x;
scanf("%d", &x);
v[x] = i;
}
std::vector<int> dp(n,0);
for (int i = 0; i < m; ++i)
{
int a,b;
//cin>>a>>b;
scanf("%d%d", &a, &b);
if(v[a]>v[b])
swap(a,b);
dp[v[b]] = max(dp[v[b]], v[a]+1);
}
for (int i = 1; i < n; ++i)
{
dp[i] = max(dp[i], dp[i-1]);
}
long long s = 0;
for (int i = 0; i < n; ++i)
{
s+=(i+1-dp[i]);
}
cout<<s;
return 0;
}
You should always try to use scanf when the amount of input is large as it is faster.
You can read more about scanf being faster here : Using scanf() in C++ programs is faster than using cin?

Code cleanup data structure

How to clean up/remove Verific data structures
int x = 15;
for (int i = 0; i < x; i++) {
mySequence[i] = 0;
}
mySequence[0] = -127;
delete[] mySequence;