# COII: Which OpenCV functions secretly change COI?

**URL:** https://forum.opencv.org/t/coii-which-opencv-functions-secretly-change-coi/1554
**Category:** Uncategorized
**Tags:** emgu, csharp
**Created:** [February 14, 2021, 10:38pm UTC](https://forum.opencv.org/t/coii-which-opencv-functions-secretly-change-coi/1554 "2021-02-14T22:38:34Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Sam\_Greenberg](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sam_greenberg/32/1017_2.png) [@Sam\_Greenberg](https://forum.opencv.org/u/Sam_Greenberg)
#### Post date: [February 14, 2021, 10:38pm UTC](https://forum.opencv.org/t/coii-which-opencv-functions-secretly-change-coi/1554/1 "2021-02-14T22:38:34Z")

</div>

I’m using OpenCV (in C# through Emgu). When I run my (~10k lines) code with MaxDegreeOfParallelism=1, everything’s great. When I run truly in parallel, I get an OpenCV Exception that Convolution (and a few other functions) don’t support COI. ☹

Apparently, something’s changing an Image[Gray,byte]'s COI to 1, doing some work, and then changing it back to 0. In serial, that’s fine. In parallel, it breaks other OpenCV functions that crash when COI≠0. I didn’t even know about COI until today, so it must be some prebuilt function in OpenCV. (Like how img.Copy subtly uses img.ROI.)

I replaced every local “img.Method(” with “img.SafeMethod(”, where I wrote the Safe one to check COI, lock img, do actual Method, unlock img, and check COI again. I replaced tons of { .CountNonzero, .Copy, .Or, .Not, .Threshold, .Sub, .And, . Convolution, .Xor }. It still breaks (only in parallel). ☹

I clearly missed an img.Method somewhere, since something’s changing an img.COI (while ignoring the img lock). Any idea which OpenCV methods change COI back and forth, so I can lock while they work?

Thanks for your help.  
-Sam

Edit: Here’s more [Info on COI](https://docs.opencv.org/2.4/modules/core/doc/old_basic_structures.html?highlight=cvcreateimage#void%20cvSetImageCOI(IplImage*%20image,%20int%20coi)). It’s an inherent property of images in OpenCV. Here’s the exception: [OpenCV: cv::Error Namespace Reference](https://docs.opencv.org/3.4/d1/d0d/namespacecv_1_1Error.html#a759fa1af92f7aa7377c76ffb142abccaa76945d22fdcd44f361e444bc59a8337f)

---

<div class="post-metadata">

### Author: ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)
#### Post date: [February 14, 2021, 11:45pm UTC](https://forum.opencv.org/t/coii-which-opencv-functions-secretly-change-coi/1554/2 "2021-02-14T23:45:28Z")

</div>

welcome.

what is “COI”? is that some C# term?

edit: I looked. it means “channel of interest”. I see _some_ signs that there is some fairly internal _function_ that’s there to be glue between C and C++. I’ve never encountered it before.

I’d say this is emgu’s “fault” for using this feature or you’re using some weird and unusual functions.

edit: I’d even say this is an emgu-only feature or emgu uses weird data structures that aren’t OpenCV-native (such as IplImage).

either way, I’d recommend NOT working concurrently on different channels of the same Mat. this does HORRIBLE things to your CPU cores’ caches.

---

<div class="post-metadata">

### Author: ![Sam\_Greenberg](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sam_greenberg/32/1017_2.png) [@Sam\_Greenberg](https://forum.opencv.org/u/Sam_Greenberg)
#### Post date: [February 15, 2021, 12:37am UTC](https://forum.opencv.org/t/coii-which-opencv-functions-secretly-change-coi/1554/3 "2021-02-15T00:37:37Z")

</div>

COI is definitely an OpenCV thing, not just Emgu. [Basic C Structures and Operations — OpenCV 2.4.13.7 documentation](https://docs.opencv.org/2.4/modules/core/doc/old_basic_structures.html?highlight=cvcreateimage#void%20cvSetImageCOI(IplImage*%20image,%20int%20coi))  
Sorry I wasn’t clear: I’m certainly not setting COI myself anywhere. Some other, prebuilt OpenCV function is momentarily editing it itself and then putting it back. We normally wouldn’t notice, but in parallel it’s causing conflicts.  
Edited the OP for more COI info.

---

<div class="post-metadata">

### Author: ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)
#### Post date: [February 15, 2021, 12:43am UTC](https://forum.opencv.org/t/coii-which-opencv-functions-secretly-change-coi/1554/4 "2021-02-15T00:43:29Z")

</div>

that’s referencing an `IplImage` type, which is VERY legacy. that’s also the C API that existed in v2.4 and has been deprecated since and removed over the years.

nothing these days should ever touch an `IplImage`. in fact I’d say it MUST NOT be done.

I’ve looked for _data structures_ that have anything relating to a “COI”. couldn’t find that.

the fields of a `cv::Mat` are documented. there’s nothing that has this functionality.

I’d like to know exemplary functions and data structures you use that cause this… also if there’s an error message and what it is and where it comes from.

you should _definitely_ talk to Emgu people about this. this forum doesn’t support Emgu even though some well-meaning responses happen sometimes. Emgu isn’t just a wrapper around current OpenCV. a lot more happens there. and _maybe_ it involves calling C APIs that should never have been used in the first place. your first instinct should be to look at Emgu for blame, not OpenCV.

> [@Sam\_Greenberg](#):
>
> It’s an inherent property of images in OpenCV

you must be mistaken. look: [Repository search results · GitHub](https://github.com/opencv/opencv/search?q=BadCOI) _one_ result, a definition that hasn’t been removed for API compatibility reasons. it is used _nowhere_.

---

<div class="post-metadata">

### Author: ![Sam\_Greenberg](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sam_greenberg/32/1017_2.png) [@Sam\_Greenberg](https://forum.opencv.org/u/Sam_Greenberg)
#### Post date: [February 15, 2021, 12:51am UTC](https://forum.opencv.org/t/coii-which-opencv-functions-secretly-change-coi/1554/5 "2021-02-15T00:51:59Z")

</div>

Very smart. That makes a lot of sense. Thanks for your help.

---

<div class="post-metadata">

### Author: ![Sam\_Greenberg](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sam_greenberg/32/1017_2.png) [@Sam\_Greenberg](https://forum.opencv.org/u/Sam_Greenberg)
#### Post date: [February 15, 2021, 1:01am UTC](https://forum.opencv.org/t/coii-which-opencv-functions-secretly-change-coi/1554/6 "2021-02-15T01:01:10Z")

</div>

I see now that my top two messages were flagged as spam! I’m sorry, I really didn’t mean to break any rules. Was it that I referenced [an OpenCV c# wrapper that shall not be named]?

---

<div class="post-metadata">

### Author: ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)
#### Post date: [February 15, 2021, 1:10am UTC](https://forum.opencv.org/t/coii-which-opencv-functions-secretly-change-coi/1554/7 "2021-02-15T01:10:21Z")

</div>

I have no idea what’s going on there. they were certainly not flagged yet when I responded earlier. I’d expect a spam filter to act immediately, so it must be someone’s deliberate action.

you gotta understand, OpenCV is primarily controlled by Intel. I think they run the forum, or have it run for them. support for third party wrappers should always be given by that third party first, not the upstream project, because they did something non-trivial (a wrapper is non-trivial) vs. resale/import/rebranding. that goes for everything in open source, also in life. asking for Emgu around here is probably considered off-topic although I wouldn’t mind it… but there aren’t any people around here who work on Emgu or have any great expertise with it, so any support would amount to “please go talk to them”.

---

<div class="post-metadata">

### Author: ![Sam\_Greenberg](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/sam_greenberg/32/1017_2.png) [@Sam\_Greenberg](https://forum.opencv.org/u/Sam_Greenberg)
#### Post date: [February 15, 2021, 1:14am UTC](https://forum.opencv.org/t/coii-which-opencv-functions-secretly-change-coi/1554/8 "2021-02-15T01:14:19Z")

</div>

Gotcha. Sorry about that. I had considered whether it was just _another_ Emgu problem, but thought it was OpenCV-related when I found the red-herring links above 🙂

Thanks for all your help.

---

<div class="post-metadata">

### Author: ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)
#### Post date: [February 15, 2021, 7:27am UTC](https://forum.opencv.org/t/coii-which-opencv-functions-secretly-change-coi/1554/9 "2021-02-15T07:27:59Z")

</div>

@Sam_Greenberg @crackwitz in previous forum emgu was not authorized only official binding. It was written in the faq. Why ? I don’t know.

Now there is nothing in faq so I don’t understand why your post is flagged.  
I’m moderator as @crackwitz but i don’t know how unflagged.  
There is a problem faq must be changed or your post must be unflagged !

---

<div class="post-metadata">

### Author: ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)
#### Post date: [February 15, 2021, 7:33am UTC](https://forum.opencv.org/t/coii-which-opencv-functions-secretly-change-coi/1554/10 "2021-02-15T07:33:42Z")

</div>



---

<div class="post-metadata">

### Author: ![laurent.berger](https://avatars.discourse-cdn.com/v4/letter/l/ec9cab/32.png) [@laurent.berger](https://forum.opencv.org/u/laurent.berger)
#### Post date: [February 15, 2021, 7:33am UTC](https://forum.opencv.org/t/coii-which-opencv-functions-secretly-change-coi/1554/11 "2021-02-15T07:33:45Z")

</div>


