使用C#与EmguCV进行二值图像漫水填充时遇种子点越界错误
Hey there, let's break down why you're hitting this error when calling CvInvoke.FloodFill() and how to fix it. This issue is straightforward once you know where to look—here are the most common culprits and solutions:
1. Your seed point coordinates are actually outside the image bounds
First, remember that EmguCV (just like OpenCV) uses 0-based indexing for image coordinates:
- The X coordinate (horizontal) ranges from
0toimage.Width - 1 - The Y coordinate (vertical) ranges from
0toimage.Height - 1
If your seed point has an X value ≥ image width, Y value ≥ image height, or either value is negative, you'll trigger this error immediately.
Quick fix: Validate the seed point before calling FloodFill
Add a simple check to ensure your seed coordinates fall within the valid range:
// Assume binaryImage is your input binary image Size imageSize = binaryImage.Size; int maxValidX = imageSize.Width - 1; int maxValidY = imageSize.Height - 1; // Let's say your seed point is (seedX, seedY) if (seedX < 0 || seedX > maxValidX || seedY < 0 || seedY > maxValidY) { // Handle the invalid point—either adjust it to a valid position or throw a meaningful error throw new ArgumentOutOfRangeException("Seed point is outside the image bounds!"); } // Now safely call FloodFill CvInvoke.FloodFill(binaryImage, new Point(seedX, seedY), new MCvScalar(255));
2. You mixed up X and Y coordinates
It's super easy to swap X (horizontal/width) and Y (vertical/height) by mistake. For example, if you pass (image.Height, image.Width) as the seed point, that's almost guaranteed to be out of bounds.
Double-check that your seed point follows the Point(x, y) format where:
x= column index (aligns with the image's width direction)y= row index (aligns with the image's height direction)
3. Your image object is invalid or empty
If your binaryImage is null, failed to load properly, or was accidentally disposed, any coordinate you pass might be treated as invalid. Always verify the image is valid before proceeding:
if (binaryImage == null || binaryImage.IsEmpty) { throw new InvalidOperationException("Input binary image is null or empty!"); }
4. You're using unadjusted UI coordinates (if working with a GUI)
If you're getting the seed point from a UI control (like a PictureBox mouse click), you need to convert the control's coordinates to the actual image coordinates—especially if the image is scaled or the control has padding/borders.
For example, in WinForms:
private void pictureBox_MouseClick(object sender, MouseEventArgs e) { PictureBox pb = sender as PictureBox; if (pb.Image == null || binaryImage == null) return; // Calculate scaling ratio between the PictureBox and the actual image float scaleX = (float)binaryImage.Width / pb.ClientSize.Width; float scaleY = (float)binaryImage.Height / pb.ClientSize.Height; // Convert mouse position to image coordinates int imageX = (int)(e.X * scaleX); int imageY = (int)(e.Y * scaleY); // Now use (imageX, imageY) as your seed point CvInvoke.FloodFill(binaryImage, new Point(imageX, imageY), new MCvScalar(255)); }
Start with the first check (validating seed coordinates)—that's the most common cause of this error. Once you rule that out, move to the other possibilities.
内容的提问来源于stack exchange,提问作者Vimukthi Guruge

