我有EMGU cv中圖像的矩陣(2D),如何用零填充矩陣的其餘部分,但是保留原始數據的某個區域(矩形)?在emgu cv中使用矩陣
1
A
回答
3
方法1
一種方式來實現你的後訪問矩陣直接的數據和設置的值是什麼「0」下面的代碼將設置「My_Matrix」的下四分之一爲0,所有其他值將保持20
Matrix<double> My_Matrix_Image = new Matrix<double>(8,10);
My_Matrix_Image.SetValue(20); //set all values of Matrix to 20
for(int i = 4; i< My_Matrix_Image.Height; i++)
{
for (int j = 5; j < My_Matrix_Image.Width; j++)
{
My_Matrix_Image.Data[i,j] = 0;
}
}
要達到什麼您從您的評論想讓你仍然必須採取同樣的做法My_Matrix_Image將包含從0-255的圖像數據值,我會爲這兩個灰階解決方案和彩色圖像。我將保留圖像的第一行30行,並將其餘設置爲零,圖像將爲尺寸爲的100x100像素。更改j = 30以更改行數。
第一顏色的圖像矩陣就會有3個通道紅,綠色和藍:
for(int i = 0; i< My_Matrix_Image.Height; i++)
{
for (int j = 30; j < My_Matrix_Image.Width; j++)
{
My_Matrix_Image.Data[i,j,0] = 0; //RED
My_Matrix_Image.Data[i,j,1] = 0; //GREEN
My_Matrix_Image.Data[i,j,2] = 0; //BLUE
}
}
灰度圖像矩陣是更容易,因爲會有1個通道:
for(int i = 0; i< My_Matrix_Image.Height; i++)
{
for (int j = 30; j < My_Matrix_Image.Width; j++)
{
My_Matrix_Image.Data[i,j,0] = 0;
}
}
現在讓我們假設你想保留中等40行你將不得不添加一個額外的循環記住這是一個矩陣的唯一途徑,我只提供了彩色圖像的例子,你可以看到它開始變得有點凌亂和方法2可能會更好:
for(int i = 0; i< My_Matrix_Image.Height; i++)
{
//LOOP 1 SET THE FRONT ROWS
for (int j = 0; j<40; j++)
{
My_Matrix_Image.Data[i,j,0] = 0; //RED
My_Matrix_Image.Data[i,j,1] = 0; //GREEN
My_Matrix_Image.Data[i,j,2] = 0; //BLUE
}
// LOOP 2 SET THE BACK ROWS
for (int j = 60; j < My_Matrix_Image.Width; j++)
{
My_Matrix_Image.Data[i,j,0] = 0; //RED
My_Matrix_Image.Data[i,j,1] = 0; //GREEN
My_Matrix_Image.Data[i,j,2] = 0; //BLUE
}
}
方法2
現在我們假設你確實想保留一個矩形的數據。創建6個循環非常複雜且效率低下,因此您可以這樣做。
//Make a Blank Copy of your Image this will be automatically full of zeros
Matrix<double> My_Image_Copy = My_Image_Matrix.CopyBlank();
//Now copy the data you want to keep from your original image into you blank copy
for(int i = 40; i< 60; i++)
{
for (int j = 40; j < 60; j++)
{
My_Image_Copy.Data[i,j,0] = My_Matrix_Image.Data[i,j,0]; //RED
My_Image_Copy.Data[i,j,1] = My_Matrix_Image.Data[i,j,1]; //GREEN
My_Image_Copy.Data[i,j,2] = My_Matrix_Image.Data[i,j,2]; //BLUE
}
}
上面的代碼將複製中心20×20像素從圖像可以很明顯的改變這種通過使用爲整個行復制(INT I = 0;我< My_Matrix_Image.Height;我++)
好多了,我相信你會同意的。
替代
現在,當您使用的是矩陣使用圖像結構,使編碼有點簡單,你存儲數據。雖然這可能與你無關,但可能對其他人有用。
如果您使用圖片或替代存儲圖像數據,那麼這可以通過以下方式實現:
Image<Gray, Byte> My_Image = new Image<Gray, byte>(openfile.FileName);
Image<Gray, Byte> My_Image_Copy = My_Image.CopyBlank();
Rectangle Store_ROI = my_image.ROI; //Only need one as both images same size
My_Image.ROI = new Rectangle(50, 50, 100, 100);
My_Image_Copy.ROI = new Rectangle(50, 50, 100, 100);
My_Image_Copy = My_Image.Copy(); //This will only copy the Region Of Interest
//Reset the Regions Of Interest so you will now operate on the whole image
My_Image.ROI = Store_ROI;
My_Image_Copy.ROI = Store_ROI;
現在這個一樣的方法2,但你並不需要整理寫出循環,其中錯誤可能發生。
希望這種修正回答你的問題,
乾杯
克里斯
1
// Rectangle's parameters
static int x = 3;
static int y = 3;
static int width = 2;
static int height = 2;
Rectangle specificArea = new Rectangle(x, y, width, height);
// Your image matrix; 20x20 just a sample
Matrix<int> imageMatrix = new Matrix<int>(20, 20);
public Matrix<int> cropMatrix()
{
// Crop a specific area from image matrix
Matrix<int> specificMatrix = imageMatrix.GetSubRect(specificArea);
// Matrix with full of zeros and same size with imageMatrix
Matrix<int> croppedMatrix = imageMatrix.CopyBlank();
for (int i = x; i < x+width; i++)
{
for (int j = y; j < y+height; j++)
{
// Set croppedMatrix with saved values
croppedMatrix[i, j] = specificMatrix[i-x, j-y];
}
}
return croppedMatrix;
}
相關問題
- 1. 矩形 - Emgu CV
- 2. EMGU中矩陣的元素方形CV
- 3. 使用emgu cv
- 4. 使用emgu CV
- 5. 如何聲明Emgu CV中的矩陣數組?
- 6. 使用Emgu CV運行相機CV
- 7. 與Emgu CV一起使用Kinect ColorImageFrame CV
- 8. 使用Emgu的Background Subtaction cv
- 9. 如何使用emgu CV
- 10. cv :: bitwise_not cv :: Mat矩陣
- 11. 在emgu cv中重塑
- 12. Emgu CV SURFFeature Error
- 13. Emgu CV ColorConversion.RGB2GRAY/BGR2GRAY
- 14. EMGU CV異常
- 15. 如何使用CvInRange在Emgu CV
- 16. 矩陣乘法使用cv ::墊
- 17. Emgu CV顯示GpuImage
- 18. Emgu CV Blob檢測
- 19. 填充emgu cv中的孔
- 20. Emgu CV中的Matrix.Reshape()函數
- 21. Emgu CV中的Alpha混合
- 22. 劃分團塊在Emgu CV
- 23. 使用Emgu CV C時發生錯誤#
- 24. 使用Emgu CV的背景減法
- 25. Emgu使用C#的CV圖像捕捉#
- 26. 使用Emgu CV C++聲明新點
- 27. 使用Emgu CV的視頻播放器
- 28. 我如何使用Emgu CV _SmoothGausian方法
- 29. 虛擬頭髮使用Emgu CV - C#
- 30. 人臉檢測算法使用Emgu CV
親愛的克里斯,這是違揹我想要的東西! 我有一個圖像的矩陣,所以我想要一些行(相鄰)保持和休息填充零。 –
嗨Reza,我已經改正了答案,給出了所有可能的方法來實現我認爲你的事。我也編輯了你的問題,讓你的問題更清楚,我希望你不介意。方法2是最簡單的我已經添加了彩色圖像,我會假設你可以改變矩陣的維度[i,j,n]到你的數據。我希望這個答案能更好的套上你在歡呼之後 – Chris
Tanx Chris,沒錯。 –