Search code examples
pythonopencvpycharmcontour

Finding the pixel value inside a threshold python


so I'm just asking if anyone knows how to find the value of the pixels insides the contour because I'm doing this project to see if any of the mangoes have damages and what I'm aiming for is to see if any pixel inside the contour exceed 100. Much Help is needed. Thank you.

This is my current code:

import cv2

from matplotlib import pyplot as plt

plt.figure(figsize=(10, 10))
img = cv2.imread('final.jpg', cv2.IMREAD_GRAYSCALE)
edges = cv2.Canny(img, 180,255)

plt.subplot(121), plt.imshow(img)
plt.title('Original Gray Scale Image')
plt.subplot(122), plt.imshow(edges)
plt.title('Edge Image')



plt.show()

The output of the code Original Image


Solution

  • I am not sure what you mean by "greater than 100", since your image is color and so has 3 values: B, G, R. But here is how to print the B,G,R values where the intensity is larger than some threshold in Python/OpenCV. I have also marked in red on the intensity image where the values are larger than some threshold, which you can change to 100 if you want. I used 190 to keep the list shorter.

    Input:

    enter image description here

    import cv2
    import numpy as np
    
    # set intensity threshold
    thresh = 190
    
    # read image
    img = cv2.imread('green_region.jpg')
    
    # convert img to grayscale intensity
    intensity = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # get img where intensity is larger than thresh
    data = img[intensity>thresh]
    
    # get coordinates where intensity is larger than thresh
    coords = np.argwhere(intensity>thresh)
    
    # print coordinates x,y vs bgr color
    for (y,x),(b,g,r)in zip(coords,data):
        print(x, y, ": ", b, g, r)
    
    # draw red pixels on intensity image where larger than thresh
    result = intensity.copy()
    result = cv2.cvtColor(result, cv2.COLOR_GRAY2BGR)
    result[intensity>thresh] = (0,0,255)
    
    # save result
    cv2.imwrite('green_region_190.jpg', result)
    
    # show thresh and result    
    cv2.imshow("intensity", intensity)
    cv2.imshow("result", result)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    

    Intensity with red for pixels larger than 190:

    enter image description here

    Pixel colors where intensity larger than 190:

    x y :  b g r
    
    574 367 :  164 212 160
    539 382 :  170 214 153
    538 383 :  170 214 153
    539 383 :  170 214 153
    539 384 :  173 214 153
    538 385 :  173 214 152
    539 385 :  173 214 153
    561 385 :  172 210 160
    538 386 :  173 214 152
    539 386 :  173 214 153
    538 387 :  173 214 152
    538 388 :  173 214 152
    536 392 :  173 214 152
    536 393 :  173 214 152
    543 398 :  173 214 153
    542 399 :  173 214 153
    543 399 :  174 215 154
    544 399 :  175 214 158
    545 399 :  174 213 157
    442 400 :  157 213 160
    535 400 :  175 215 150
    552 400 :  177 212 155
    535 401 :  176 216 151
    535 402 :  177 217 152
    536 402 :  175 214 152
    534 403 :  175 215 150
    535 403 :  178 218 153
    536 403 :  175 214 152
    537 403 :  175 214 152
    534 404 :  176 216 151
    535 404 :  178 218 153
    536 404 :  175 214 152
    537 404 :  175 214 152
    534 405 :  176 216 151
    535 405 :  178 218 153
    536 405 :  175 214 152
    534 406 :  176 216 151
    535 406 :  177 217 152
    534 407 :  175 215 150
    535 407 :  177 217 152
    532 408 :  175 215 150
    533 408 :  175 215 150
    534 408 :  176 216 151
    535 408 :  176 216 151
    532 409 :  175 215 150
    533 409 :  175 215 150
    534 409 :  176 216 151
    535 409 :  176 216 151
    532 410 :  175 215 150
    533 410 :  175 215 150
    534 410 :  176 216 151
    535 410 :  176 216 151
    532 411 :  175 215 150
    533 411 :  175 215 150
    534 411 :  176 216 151
    535 411 :  176 216 151
    532 412 :  175 215 150
    533 412 :  175 215 150
    534 412 :  176 216 151
    535 412 :  176 216 151
    532 413 :  175 215 150
    533 413 :  175 215 150
    534 413 :  176 216 151
    535 413 :  176 216 151
    532 414 :  175 215 150
    533 414 :  175 215 150
    534 414 :  176 216 151
    535 414 :  176 216 151
    536 414 :  175 214 152
    537 414 :  175 214 152
    538 414 :  173 214 152
    455 415 :  154 213 162
    525 415 :  173 217 148
    526 415 :  174 218 149
    527 415 :  174 218 149
    532 415 :  175 215 150
    533 415 :  175 215 150
    534 415 :  176 216 151
    535 415 :  176 216 151
    536 415 :  176 215 153
    537 415 :  176 215 153
    538 415 :  174 215 153
    539 415 :  173 214 152
    540 415 :  173 214 152
    529 416 :  175 213 153
    530 416 :  175 213 153
    531 416 :  175 213 153
    532 416 :  175 213 153
    533 416 :  175 213 153
    536 416 :  175 213 153
    537 416 :  175 213 153
    527 417 :  177 214 150
    529 417 :  175 213 153
    530 417 :  175 213 153
    531 417 :  175 213 153
    532 417 :  175 213 153
    533 417 :  175 213 153
    536 417 :  175 213 153
    537 417 :  175 213 153
    538 417 :  175 213 153
    526 418 :  178 216 150
    527 418 :  179 216 152
    528 418 :  175 213 153
    529 418 :  175 213 153
    530 418 :  175 213 153
    531 418 :  175 213 153
    532 418 :  175 213 153
    533 418 :  175 213 153
    536 418 :  176 214 154
    537 418 :  175 213 153
    538 418 :  175 213 153
    539 418 :  175 213 153
    526 419 :  177 215 149
    527 419 :  178 215 151
    528 419 :  175 213 153
    529 419 :  175 213 153
    530 419 :  175 213 153
    531 419 :  175 213 153
    532 419 :  175 213 153
    533 419 :  175 213 153
    534 419 :  175 213 153
    535 419 :  175 213 153
    536 419 :  176 214 154
    537 419 :  176 214 154
    538 419 :  175 213 153
    539 419 :  175 213 153
    528 420 :  176 214 154
    529 420 :  176 214 154
    530 420 :  176 214 154
    531 420 :  176 214 154
    532 420 :  176 214 154
    533 420 :  176 214 154
    534 420 :  175 213 153
    535 420 :  175 213 153
    536 420 :  176 214 154
    537 420 :  176 214 154
    538 420 :  176 214 154
    539 420 :  175 213 153
    528 421 :  176 214 154
    529 421 :  176 214 154
    530 421 :  177 215 155
    531 421 :  177 215 155
    532 421 :  177 215 155
    533 421 :  177 215 155
    534 421 :  176 214 154
    535 421 :  176 214 154
    536 421 :  177 215 155
    537 421 :  177 215 155
    538 421 :  176 214 154
    539 421 :  175 213 153
    528 422 :  176 215 153
    529 422 :  177 216 154
    530 422 :  178 217 155
    531 422 :  178 217 155
    532 422 :  178 217 155
    533 422 :  178 217 155
    534 422 :  177 216 154
    535 422 :  176 215 153
    536 422 :  178 217 155
    537 422 :  178 217 155
    538 422 :  177 216 154
    539 422 :  176 215 153
    540 422 :  175 214 152
    528 423 :  176 215 153
    529 423 :  177 216 154
    530 423 :  178 217 155
    531 423 :  179 218 156
    532 423 :  179 218 156
    533 423 :  178 217 155
    534 423 :  177 216 154
    535 423 :  176 215 153
    536 423 :  179 218 156
    537 423 :  178 217 155
    538 423 :  178 217 155
    539 423 :  177 216 154
    540 423 :  176 215 153
    544 423 :  177 214 150
    529 424 :  175 214 152
    530 424 :  178 217 155
    531 424 :  178 217 155
    532 424 :  178 217 155
    533 424 :  177 216 154
    534 424 :  177 216 154
    535 424 :  177 216 154
    536 424 :  177 216 154
    537 424 :  177 216 154
    538 424 :  176 215 153
    539 424 :  175 214 152
    540 424 :  175 214 152
    528 425 :  175 214 152
    529 425 :  177 216 154
    530 425 :  178 217 155
    531 425 :  179 218 156
    532 425 :  177 216 154
    533 425 :  176 215 153
    534 425 :  176 215 153
    535 425 :  177 216 154
    536 425 :  176 215 153
    537 425 :  176 215 153
    538 425 :  175 214 152
    539 425 :  175 214 152
    525 426 :  177 215 149
    528 426 :  176 215 153
    529 426 :  178 217 155
    530 426 :  179 218 156
    531 426 :  178 217 155
    532 426 :  176 215 153
    533 426 :  175 214 152
    534 426 :  175 214 152
    535 426 :  175 214 152
    536 426 :  175 214 152
    537 426 :  175 214 152
    524 427 :  177 215 149
    525 427 :  178 216 150
    526 427 :  177 215 149
    528 427 :  177 217 152
    529 427 :  178 218 153
    530 427 :  178 218 153
    531 427 :  177 217 152
    504 428 :  168 217 149
    524 428 :  177 215 149
    525 428 :  178 216 150
    526 428 :  178 216 150
    528 428 :  176 216 151
    529 428 :  176 216 151
    530 428 :  176 216 151
    504 429 :  168 217 149
    510 429 :  172 218 146
    511 429 :  172 218 146
    524 429 :  177 215 149
    525 429 :  178 216 150
    526 429 :  177 215 149
    528 429 :  175 215 150
    529 429 :  175 215 150
    530 429 :  175 215 150
    496 430 :  164 215 155
    497 430 :  164 215 155
    510 430 :  172 218 146
    511 430 :  172 218 146
    524 430 :  177 215 149
    525 430 :  178 216 150
    526 430 :  177 215 149
    528 430 :  175 215 150
    529 430 :  175 215 150
    496 431 :  165 216 156
    497 431 :  164 215 155
    510 431 :  172 218 146
    511 431 :  172 218 146
    525 431 :  177 215 149
    528 431 :  175 215 150
    529 431 :  175 215 150
    530 431 :  175 215 150
    499 432 :  166 216 152
    500 432 :  167 217 153
    501 432 :  168 215 152
    527 432 :  177 215 149
    528 432 :  175 214 152
    534 432 :  175 215 150
    535 432 :  176 216 151
    499 433 :  167 217 153
    500 433 :  167 217 153
    501 433 :  168 215 152
    535 433 :  175 215 150
    497 434 :  166 215 153
    498 434 :  167 217 153
    499 434 :  168 218 154
    500 434 :  167 217 153
    501 434 :  168 215 152
    496 435 :  167 216 154
    497 435 :  167 217 153
    498 435 :  168 218 154
    499 435 :  168 218 154
    500 435 :  167 217 153
    496 436 :  167 217 153
    497 436 :  167 217 153
    498 436 :  167 217 153
    499 436 :  166 216 152
    496 437 :  166 216 152
    496 440 :  166 217 150
    512 440 :  175 216 148
    519 440 :  178 216 150
    523 440 :  177 215 149
    524 440 :  178 216 150
    525 440 :  178 216 150
    526 440 :  178 216 150
    527 440 :  178 216 150
    519 441 :  177 215 149
    523 441 :  177 215 149
    524 441 :  178 216 150
    525 441 :  178 216 150
    526 441 :  177 215 149
    527 441 :  177 215 149
    523 442 :  177 215 149
    524 442 :  177 215 149
    525 442 :  177 215 149
    512 447 :  176 217 149
    513 447 :  175 216 148
    482 448 :  163 215 155
    483 448 :  165 216 156
    520 448 :  177 218 150
    521 448 :  176 217 149
    512 449 :  175 216 148
    513 449 :  175 216 148
    520 449 :  176 217 149
    513 450 :  175 216 148
    477 454 :  159 216 155
    520 454 :  176 217 149
    477 455 :  159 216 155
    514 455 :  175 216 148
    515 455 :  176 217 149
    516 455 :  176 217 149
    517 455 :  175 216 148
    518 455 :  175 216 148
    519 455 :  175 216 148
    520 455 :  177 218 150
    521 455 :  176 217 149
    475 456 :  161 218 157
    476 456 :  162 219 158
    477 456 :  160 217 156
    512 456 :  175 216 148
    513 456 :  175 216 148
    514 456 :  176 217 149
    515 456 :  177 218 150
    516 456 :  178 219 151
    517 456 :  177 218 150
    518 456 :  177 218 150
    519 456 :  177 218 150
    520 456 :  178 219 151
    521 456 :  178 219 151
    522 456 :  178 219 151
    523 456 :  177 218 150
    474 457 :  159 216 155
    475 457 :  160 217 156
    476 457 :  160 217 156
    513 457 :  175 216 148
    514 457 :  176 217 149
    515 457 :  177 218 150
    516 457 :  177 218 150
    517 457 :  177 218 150
    518 457 :  177 218 150
    519 457 :  176 217 149
    520 457 :  177 218 150
    521 457 :  177 218 150
    522 457 :  177 218 150
    523 457 :  175 216 148
    514 458 :  175 216 148
    515 458 :  176 217 149
    516 458 :  176 217 149
    517 458 :  176 217 149
    518 458 :  176 217 149
    519 458 :  176 217 149
    520 458 :  175 216 148
    521 458 :  175 216 148
    522 458 :  175 216 148
    514 459 :  175 216 148
    515 459 :  175 216 148
    516 459 :  176 217 149
    517 459 :  176 217 149
    518 459 :  175 216 148
    519 459 :  175 216 148
    515 460 :  175 216 148
    516 460 :  175 216 148
    517 460 :  175 216 148
    444 488 :  150 215 160
    444 489 :  151 216 161
    444 490 :  151 216 161