 
 		d_img_labels_.create(d_img_.size(), CV_32SC1);
 
+		last_pixel_allocated_ = false;
+		if ((d_img_.rows == 1 || d_img_.cols == 1) && !((d_img_.rows + d_img_.cols) % 2)) {
+			cudaMalloc(&last_pixel_, sizeof(unsigned char));
+			last_pixel_allocated_ = true;
+		}
+		else {
+			// last_pixel_ = d_img_labels_.data + d_img_labels_.step + sizeof(unsigned int);
+			last_pixel_ = d_img_labels_.data + ((d_img_labels_.rows - 2) * d_img_labels_.step) + (d_img_labels_.cols - 2) * d_img_labels_.elemSize();
+		}
+
 		grid_size_ = dim3((((d_img_.cols + 1) / 2) + BLOCK_COLS - 1) / BLOCK_COLS, (((d_img_.rows + 1) / 2) + BLOCK_ROWS - 1) / BLOCK_ROWS, 1);
 		block_size_ = dim3(BLOCK_COLS, BLOCK_ROWS, 1);
 
-		InitLabeling << <grid_size_, block_size_ >> > (d_img_, d_img_labels_);
+		InitLabeling << <grid_size_, block_size_ >> > (d_img_, d_img_labels_, last_pixel_);
+
+		//Mat1i init_blocks;
+		//d_img_labels_.download(init_blocks);
+
+		//cuda::GpuMat d_init_labels = d_img_labels_.clone();
+		//FinalLabeling << <grid_size_, block_size_ >> > (d_img_, d_init_labels);
+		//Mat1i init_labels;
+		//d_init_labels.download(init_labels);
+		//d_init_labels.release();
 
 		Compression << <grid_size_, block_size_ >> > (d_img_labels_);
 
-		//Mat1i init_labels;
-		//d_img_labels_.download(init_labels);
+		//Mat1i compr_blocks;
+		//d_img_labels_.download(compr_blocks);
+
+		//cuda::GpuMat d_compr_labels = d_img_labels_.clone();
+		//FinalLabeling << <grid_size_, block_size_ >> > (d_img_, d_compr_labels);
+		//Mat1i compr_labels;
+		//d_compr_labels.download(compr_labels);
+		//d_compr_labels.release();
 
 		//cuda::GpuMat d_expanded_connections;
 		//d_expanded_connections.create(d_connections_.rows * 3, d_connections_.cols * 3, CV_8UC1);

		grid_size_ = dim3((((d_img_.cols + 1) / 2) + BLOCK_COLS - 1) / BLOCK_COLS, (((d_img_.rows + 1) / 2) + BLOCK_ROWS - 1) / BLOCK_ROWS, 1);
		block_size_ = dim3(BLOCK_COLS, BLOCK_ROWS, 1);

		InitLabeling << <grid_size_, block_size_ >> > (d_img_, d_img_labels_);

		Compression << <grid_size_, block_size_ >> > (d_img_labels_);



		if (row < labels.rows && col < labels.cols) {

			unsigned int label = labels[labels_index] + 1;

			if (img.data[img_index])
				labels[labels_index] = label;
			else {
				labels[labels_index] = 0;
			}

			if (col + 1 < labels.cols) {
				if (img.data[img_index + 1])
					labels[labels_index + 1] = label;
				else {
					labels[labels_index + 1] = 0;
				}

				if (row + 1 < labels.rows) {
					if (img.data[img_index + img.step + 1])
						labels[labels_index + (labels.step / labels.elem_size) + 1] = label;
					else {
						labels[labels_index + (labels.step / labels.elem_size) + 1] = 0;
					}
				}
			}

			if (row + 1 < labels.rows) {
				if (img.data[img_index + img.step])
					labels[labels_index + (labels.step / labels.elem_size)] = label;
				else {
					labels[labels_index + (labels.step / labels.elem_size)] = 0;
				}
			}

		}
