Skin cancer is a common disease resulting from genetic defects, and early detection is critical to improve treatment outcomes. Diagnostic programs that use computer aid especially those that use supervised learning are very useful in early diagnosis of skin cancer. This research therefore presents a new approach that integrates optimization methods with supervised learning to improve skin cancer diagnosis using machine vision approach. The presented method is initiated by data pre-processing that involves the removal of unnecessary data. Then, to segment the images, a combination of K-means clustering and social spider optimization technique is employed. The region of interest is then extracted from the segmented image and from this region a convolutional neural network extracts the significant features. To enhance the classification performance as compared with the standard classifiers, this research introduces a new concept of error correcting output codes coupled with a weighted Hamming distance in the group of gamma classifiers. The ability of the proposed approach in segmentation of skin lesions and classifying them was tested using samples from the ISIC-2017 and ISIC-2016 databases. The introduced method obtained state-of-the-art accuracy on both datasets (ISIC-2016: 97.10%, ISIC-2017: 95.17%). In particularly, the accuracy of the introduced approach for both these databases is at least 1.17% higher than the compared methods. This proves the high performance of the suggested method based on the usage of the convolutional neural networks for feature extraction and gamma classifiers with error correcting output codes for classification in skin cancer detection.